CentOS 7 下搭建 Docker 服务器

环境和软件版本:

$ docker --version
Docker version 20.10.0, build 7287ab3
$ dockerd --version
Docker version 20.10.0, build eeddea2
$ cat /etc/centos-release
CentOS Linux release 7.6.1810 (Core)

一、安装过程

切换到 root 账号,执行如下命令:

# 1. 卸载旧版 docker
yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine
# 2. 设置 docker 源
#   2.1 包 yum-utils 中含有 yum-config-manager 命令,可以方便地设置仓库
yum install -y yum-utils
#   2.2 添加仓库。
yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo
# 3. 安装最新版 docker 服务
yum install docker-ce docker-ce-cli containerd.io

# 4. 启动服务,查看服务状态
#   4.1 启动服务
systemctl start docker
#   4.2 查看服务是否启动
systemctl status docker
#   4.3 执行客户端命令试试
docker version
#   4.4 设置服务开机启动
systemctl enable docker
# 5. 配置docker国内镜像
tee /etc/docker/daemon.json <<-'EOF'
{

  "registry-mirrors": ["https://uxk0ognt.mirror.aliyuncs.com"]

}
EOF
systemctl restart docker
# 6. 把普通用户 chenx 加入docker组,使其可以执行 docker 命令
useradd docker -r -g docker
# 7. 运行镜像
docker run hello-world

二、安装过程解释

1. 卸载旧版 docker

2 设置 docker 源

yum官方维护的docker是旧版本的,新版docker在docker维护着,所以需要配置一下yum源。

2.1 安装 yum-utils 命令

yum info yum-utils查看此包的说明
rpm -ql yum-utils 查看这个包具体安装了哪些文件,后面再接一个 grep 就能过滤到安装了哪些命令。

2.2 添加镜像文

/etc/yum.repos.d/ 目录下添加了一个文件 docker-ce.repo
每个文件是都是一个仓库配置文件,可以配置多个仓库。

$ ls /etc/yum.repos.d/
CentOS-Base.repo  docker-ce.repo  epel.repo

查看这个文件的内容:cat /etc/yum.repos.d/docker-ce.repo

yum repolistyum repolist all 查看当前的源有多少个。

[root@chenxizhan yum.repos.d]# yum repolist
repo id                              repo name                                                  status
base/7/x86_64                        CentOS-7                                                   10,072
docker-ce-stable/7/x86_64            Docker CE Stable - x86_64                                      89
epel/x86_64                          Extra Packages for Enterprise Linux 7 - x86_64             13,486
extras/7/x86_64                      CentOS-7                                                      448
updates/7/x86_64                     CentOS-7                                                      778
repolist: 24,873
[root@chenxizhan yum.repos.d]# yum repolist all
repo id                               repo name                                        status
base/7/x86_64                         CentOS-7                                         enabled: 10,072
docker-ce-nightly/7/x86_64            Docker CE Nightly - x86_64                       disabled
docker-ce-nightly-debuginfo/7/x86_64  Docker CE Nightly - Debuginfo x86_64             disabled
docker-ce-nightly-source/7            Docker CE Nightly - Sources                      disabled
docker-ce-stable/7/x86_64             Docker CE Stable - x86_64                        enabled:     89
docker-ce-stable-debuginfo/7/x86_64   Docker CE Stable - Debuginfo x86_64              disabled
docker-ce-stable-source/7             Docker CE Stable - Sources                       disabled
docker-ce-test/7/x86_64               Docker CE Test - x86_64                          disabled
docker-ce-test-debuginfo/7/x86_64     Docker CE Test - Debuginfo x86_64                disabled
docker-ce-test-source/7               Docker CE Test - Sources                         disabled
epel/x86_64                           Extra Packages for Enterprise Linux 7 - x86_64   enabled: 13,486
extras/7/x86_64                       CentOS-7                                         enabled:    448
updates/7/x86_64                      CentOS-7                                         enabled:    778
repolist: 24,873

可以看到,添加了 8 个 docker 相关的源,并默认启用了 stable 源。

test 是测试的。
nightly 比测试版更新的版本。

3. 安装最新版 docker 服务

三个包:containerd.io、docker-ce、docker-ce-cli。
docker-ce-cli 是客户端,containerd.io 和 docker-ce 是服务端。

docker-ce 依赖 docker-ce-clidontainerd.io
yum deplist 可以查看依赖关系,验证一下:

# yum deplist docker-ce | grep 'docker\|con'
package: docker-ce.x86_64 3:20.10.0-3.el7
  dependency: container-selinux >= 2:2.74
   provider: container-selinux.noarch 2:2.119.2-1.911c772.el7_8
  dependency: containerd.io >= 1.4.1
   provider: containerd.io.x86_64 1.4.3-3.1.el7
  dependency: docker-ce-cli
   provider: docker-ce-cli.x86_64 1:20.10.0-3.el7
  dependency: docker-ce-rootless-extras
   provider: docker-ce-rootless-extras.x86_64 20.10.0-3.el7

又因为 yum 会自动安装软件依赖,所以这个安装命令其实可以简写为yum install docker-ce

4. 启动服务,查看服务状态

# 4. 启动服务,查看服务状态
#   4.1 启动服务
systemctl start docker
#   4.2 查看服务是否启动
systemctl status docker
#   4.3 执行客户端命令试试
docker version
#   4.4 设置服务开机启动
systemctl enable docker
#   4.2 查看服务是否启动
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 Sun 2020-12-13 19:12:36 CST; 2min 9s ago
     Docs: https://docs.docker.com

看到 active (running) 说明服务启动了。

systemctl start dockersystemctl enable docker等价于一条命令:
systemctl enable --now docker

5. 配置docker国内镜像

国内访问 Docker 的官方仓库很慢,还经常断线,所以设置一下国内镜像。

tee /etc/docker/daemon.json <<-'EOF'
{

  "registry-mirrors": ["https://uxk0ognt.mirror.aliyuncs.com"]

}
EOF

配置完成之后,文件内容如下cat /etc/docker/daemon.json:

{

  "registry-mirrors": ["https://uxk0ognt.mirror.aliyuncs.com"]

}

不同的docker版本,其配置文件路径可能不一样,具体查看官方手册。

TODO:具体的配置文件路径。

6. 把普通用户 chenx 加入docker组,使其可以执行 docker 命令

useradd docker -r -g docker 这样,日常使用 docker 的时候使用非特权用户就行了,更安全一些。

6. 运行镜像

# 7. 运行镜像
$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:1a523af650137b8accdaed439c17d684df61ee4d74feac151b5b337bd29e7eec
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/

看到

Hello from Docker!
This message shows that your installation appears to be working correctly.

说明运行成功。