Docker(二):在 CentOS 中安装 Docker CE
关于在各平台安装 Docker 的方法,官方文档都有详细说明,本文仅以在 CentOS 中安装 Docker CE 为例,因为演示的需要,本文的 CentOS 又是安装在 VMware 虚拟机中的。
先决条件
系统要求:
- CentOS 7
- 内存建议 4 G 或以上
使用 Docker 仓库安装
设置 Docker 仓库
1、安装所需要的包:
$ sudo yum install -y yum-utils device-mapper-persistent-data lvm2
2、设置 stable 版本的 Docker 仓库:
$ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
安装 Docker
1、列出仓库中可用的 Docker CE 版本,并按版本号从高到低排序:
$ yum list docker-ce --showduplicates | sort -r
* updates: mirrors.cn99.com
Loaded plugins: fastestmirror
* extras: mirrors.cn99.com
docker-ce.x86_64 3:18.09.6-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.5-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.4-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.3-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.2-3.el7 docker-ce-stable
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.3.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.2.ce-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.x86_64 18.03.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 18.03.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.12.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.12.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.09.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.09.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.06.2.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.06.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.06.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.3.ce-1.el7 docker-ce-stable
docker-ce.x86_64 17.03.2.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.0.ce-1.el7.centos docker-ce-stable
Determining fastest mirrors
* base: mirrors.aliyun.com
Available Packages
2、通过完全限定的包名来安装特定版本的 Docker CE,其中版本号是上述第二列中,从第一个冒号(:
)开始,直到第一个连字符(-
)之间的数字字符串:
$ sudo yum install -y docker-ce-18.09.6 docker-ce-cli-18.09.6 containerd.io
如果不指定版本号,则默认安装最新版本:
$ sudo yum install -y docker-ce docker-ce-cli containerd.io
此时,Docker 已安装但尚未启动,并自动创建了 docker
用户组,但未向该组添加任何用户。
查看是否存在
docker
用户组:
$ getent group | grep docker docker:x:995:
列出
docker
用户组下的所有用户:
$ sudo lid -g docker
3、启动 Docker:
$ sudo systemctl start docker
当然我们可以通过以下命令查看 Docker 是否已经成功启动:
$ sudo systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
Active: active (running) since Wed 2019-06-19 18:20:56 CST; 5min ago
Docs: https://docs.docker.com
Main PID: 10390 (dockerd)
Tasks: 10
Memory: 31.6M
CGroup: /system.slice/docker.service
└─10390 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
Jun 19 18:20:55 localhost.localdomain dockerd[10390]: time="2019-06-19T18:20:55.687739430+08:00" level=info msg="pickfirstBalancer: HandleSubConnStateChange: 0xc420045410, READY" module=grpc
Jun 19 18:20:55 localhost.localdomain dockerd[10390]: time="2019-06-19T18:20:55.694689295+08:00" level=info msg="pickfirstBalancer: HandleSubConnStateChange: 0xc4200451e0, READY" module=grpc
Jun 19 18:20:55 localhost.localdomain dockerd[10390]: time="2019-06-19T18:20:55.730978238+08:00" level=info msg="Graph migration to content-addressability took 0.00 seconds"
Jun 19 18:20:55 localhost.localdomain dockerd[10390]: time="2019-06-19T18:20:55.732295301+08:00" level=info msg="Loading containers: start."
Jun 19 18:20:56 localhost.localdomain dockerd[10390]: time="2019-06-19T18:20:56.081086755+08:00" level=info msg="Default bridge (docker0) is assigned with an IP address 172.17.0.0/16. Daemon option --bip can be used to set a preferred IP address"
Jun 19 18:20:56 localhost.localdomain dockerd[10390]: time="2019-06-19T18:20:56.346977817+08:00" level=info msg="Loading containers: done."
Jun 19 18:20:56 localhost.localdomain dockerd[10390]: time="2019-06-19T18:20:56.375133477+08:00" level=info msg="Docker daemon" commit=481bc77 graphdriver(s)=overlay2 version=18.09.6
Jun 19 18:20:56 localhost.localdomain dockerd[10390]: time="2019-06-19T18:20:56.375598418+08:00" level=info msg="Daemon has completed initialization"
Jun 19 18:20:56 localhost.localdomain dockerd[10390]: time="2019-06-19T18:20:56.454094942+08:00" level=info msg="API listen on /var/run/docker.sock"
Jun 19 18:20:56 localhost.localdomain systemd[1]: Started Docker Application Container Engine.
因为一般情况下,我们都是希望在系统启动时就能够帮我们自动运行 Docker 服务,故可以执行以下命令设置开机启动时自动启动 Docker 服务:
$ sudo systemctl enable docker
如需禁止开机自动启动 Docker 服务,则执行以下命令即可:
$ sudo systemctl disable docker
4、验证 Docker CE 是否安装成功。执行以下命令,从远程镜像服务中将拉取 hello-world
镜像,并执行:
$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:41a65640635299bab090f783209c1e3a3f11934cf7756b09cb2f1e02147c6ed8
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
上述命令会下载一个测试镜像并在容器中运行它。当容器运行时,它打印一条信息消息并退出。
Docker CE 已安装并正在运行。但在这里,当我们以非 root
用户登录时,需要使用 sudo
才能运行 docker
命令。如通过以下命令列出本地容器:
$ sudo docker ps
那有什么办法,能够不需要 sudo
,即可让我们当前普通用户能够直接运行 docker
命令呢?
欲知后事如何,且听下回分解。