简介
Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从 Apache2.0 协议开源。
Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。
容器是完全使用沙箱机制,相互之间不会有任何接口(类似 iPhone 的 app),更重要的是容器性能开销极低。
Docker 从 17.03 版本之后分为 CE(Community Edition: 社区版) 和 EE(Enterprise Edition: 企业版),我们用社区版就可以了。
CentOS Docker安装
支持版本
Docker 支持以下的 64 位 CentOS 版本:
-
CentOS 7
-
CentOS 7
-
CentOS 8
-
更高版本...
使用官方安装脚本自动安装
安装命令如下:
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
也可以使用国内 daocloud 一键安装命令:
curl -sSL https://get.daocloud.io/docker | sh
手动安装
卸载旧版本呢
较旧的 Docker 版本称为 docker 或 docker-engine 。如果已安装这些程序,请卸载它们以及相关的依赖项。
[zhouhuo@zhouhuo ~]$ sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
安装 Docker Engine-Community
使用 Docker 仓库进行安装
在新主机上首次安装 Docker Engine-Community 之前,需要设置 Docker 仓库。之后,您可以从仓库安装和更新 Docker。
设置仓库
安装所需的软件包。yum-utils 提供了 yum-config-manager ,并且 device mapper 存储驱动程序需要 device-mapper-persistent-data 和 lvm2。
[zhouhuo@zhouhuo ~]$ sudo yum install -y yum-utils device-mapper-persistent-data lvm2
使用以下命令来设置稳定的仓库。
[zhouhuo@zhouhuo ~]$ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
可以选择国内的一些源地址:
阿里云
[zhouhuo@zhouhuo ~]$ sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
清华大学源
[zhouhuo@zhouhuo ~]$ sudo yum-config-manager --add-repo https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
安装 Docker Engine-Community
安装最新版本的 Docker Engine-Community 和 containerd,或者转到下一步安装特定版本:
[zhouhuo@zhouhuo ~]$ sudo yum install docker-ce docker-ce-cli containerd.io
如果提示您接受 GPG 密钥,请选是。
有多个 Docker 仓库吗?
如果启用了多个 Docker 仓库,则在未在 yum install 或 yum update 命令中指定版本的情况下,进行的安装或更新将始终安装最高版本,这可能不适合您的稳定性需求。
Docker 安装完默认未启动。并且已经创建好 docker 用户组,但该用户组下没有用户。
要安装特定版本的 Docker Engine-Community,请在存储库中列出可用版本,然后选择并安装:
-
列出并排序您存储库中可用的版本。此示例按版本号(从高到低)对结果进行排序。
[zhouhuo@zhouhuo ~]$ yum list docker-ce --showduplicates | sort -r docker-ce.x86_64 3:18.09.1-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.0-3.el7 docker-ce-stable docker-ce.x86_64 18.06.1.ce-3.el7 docker-ce-stable docker-ce.x86_64 18.06.0.ce-3.el7 docker-ce-stable
-
通过其完整的软件包名称安装特定版本,该软件包名称是软件包名称(docker-ce)加上版本字符串(第二列),从第一个冒号(:)一直到第一个连字符,并用连字符(-)分隔。例如:docker-ce-18.09.1。
[zhouhuo@zhouhuo ~]$ sudo yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io
启动 Docker。
[zhouhuo@zhouhuo ~]$ sudo systemctl start docker
通过运行 hello-world 映像来验证是否正确安装了 Docker Engine-Community 。
[zhouhuo@zhouhuo ~]$ sudo docker run hello-world
镜像加速
国内从 DockerHub 拉取镜像有时会遇到困难,此时可以配置镜像加速器。Docker 官方和国内很多云服务商都提供了国内加速器服务,例如:
- 网易:https://hub-mirror.c.163.com/
- 阿里云:https://<你的ID>.mirror.aliyuncs.com
- 七牛云加速器:https://reg-mirror.qiniu.com
当配置某一个加速器地址之后,若发现拉取不到镜像,请切换到另一个加速器地址。国内各大云服务商均提供了 Docker 镜像加速服务,建议根据运行 Docker 的云平台选择对应的镜像加速服务。
阿里云镜像获取地址:https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors
登陆后,左侧菜单选中镜像加速器就可以看到你的专属地址了:
Ubuntu14.04、Debian7Wheezy
对于使用 upstart 的系统而言,编辑 /etc/default/docker 文件,在其中的 DOCKER_OPTS 中配置加速器地址:
DOCKER_OPTS="--registry-mirror=https://registry.docker-cn.com"
重新启动服务:
[zhouhuo@zhouhuo ~]$ sudo service docker restart
Ubuntu16.04+、Debian8+、CentOS7
对于使用 systemd 的系统,请在 /etc/docker/daemon.json 中写入如下内容(如果文件不存在请新建该文件):
# 根据获取到的加速地址替换
{"registry-mirrors":["https://reg-mirror.qiniu.com/"]}
最后重新启动服务:
[zhouhuo@zhouhuo ~]$ sudo systemctl daemon-reload
[zhouhuo@zhouhuo ~]$ sudo systemctl restart docker
Docker命令自动补齐
机制
Linux 系统许多命令都会提供该命令自身的命令补齐脚本,在安装命令时会自动安装自动补齐脚本,如果有的话,该机制在Linux中被称为 bash-complete。
在 /usr/share/bash-completion/completions/ 目录下有许多命令自动补齐的脚本,可自行查阅。
与 docker 有关的,有 2 个文件: docker 和 docker-compose。如下:
[zhouhuo@zhouhuo ~]$ ls /usr/share/bash-completion/completions/docker*
/usr/share/bash-completion/completions/docker
/usr/share/bash-completion/completions/docker-compose
(**备注:**如果没有安装 docker compose,那么只有一个 docker 自动补齐脚本)
自动补齐需要依赖工具 bash-complete,如果没有,则需要手动安装,命令如下:
[zhouhuo@zhouhuo ~]$ yum -y install bash-completion
安装成功后,得到文件为 /usr/share/bash-completion/bash_completion ,如果没有这个文件,则说明系统上没有安装这个工具。
安装
让配置脚本生效的方法是执行 source xxx 命令,先执行 source /usr/share/bash-completion/completions/docker,再输入 docker,然后按2次 Tab键,提示错误如下:
[zhouhuo@zhouhuo ~]$ docker (docker + 空格 + 连续按2次Tab键)
docker bash: _get_comp_words_by_ref: command not found
bash: [: : integer expression expected
bash: [: : integer expression expected
bash: [: : integer expression expected
bash: _get_comp_words_by_ref: command not found
bash: [: : integer expression expected
bash: [: : integer expression expected
bash: [: : integer expression expected
注:第一行的docker,实际是输入的命令,其它是按Tab键的输出信息。
前面已经安装了 bash_completion,执行如下命令:
[zhouhuo@zhouhuo ~]$ source /usr/share/bash-completion/bash_completion
再次尝试,发现可以正常列出docker的子命令,示例如下:
[zhouhuo@zhouhuo ~]$ docker (docker + 空格 + 连续按2次Tab键)
attach container engine history inspect logs port restart search stats top volume
build context events image kill network ps rm secret stop trust wait
builder cp exec images load node pull rmi service swarm unpause
commit create export import login pause push run stack system update
config diff help info logout plugin rename save start tag version
尝试 Docker 容器名称的自动补齐功能,示例如下:
[root@zhouhuo ~]# docker c
commit config container context cp create
若安装了 Docker Compose,则也可事实 docker-compose 命令的自动补齐功能。
重启系统后,也能进行自动补齐。
引用说明
- 本文引用了菜鸟教程的文章 Docker 教程
- 本文引用了morgan363的文章Docker笔记7:Docker 命令自动补齐
评论区