CentOS安装docker

注意:不要采用yum直接安装CentOS repository自带的docker。

1. 安装

先卸载已有的docker环境:

1
2
3
4
5
6
7
8
9
10
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-selinux \
docker-engine-selinux \
docker-engine

安装所需的软件包。yum-utils提供了yum-config-manager功能,而device-mapper-persistent-datalvm2devicemapper存储驱动程序所需。

1
2
3
yum install -y yum-utils \
device-mapper-persistent-data \
lvm2

使用下面命令设置 docker 的稳定版本仓库。

1
2
3
# yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo

会在/etc/yum.repos.d/docker-ce.repo中添加如下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
[docker-ce-stable]
name=Docker CE Stable - $basearch
baseurl=https://download.docker.com/linux/centos/7/$basearch/stable
enabled=1
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg

[docker-ce-stable-debuginfo]
name=Docker CE Stable - Debuginfo $basearch
baseurl=https://download.docker.com/linux/centos/7/debug-$basearch/stable
enabled=0
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg

[docker-ce-stable-source]
name=Docker CE Stable - Sources
baseurl=https://download.docker.com/linux/centos/7/source/stable
enabled=0
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg

[docker-ce-edge]
name=Docker CE Edge - $basearch
baseurl=https://download.docker.com/linux/centos/7/$basearch/edge
enabled=0
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg

[docker-ce-edge-debuginfo]
name=Docker CE Edge - Debuginfo $basearch
baseurl=https://download.docker.com/linux/centos/7/debug-$basearch/edge
enabled=0
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg

[docker-ce-edge-source]
name=Docker CE Edge - Sources
baseurl=https://download.docker.com/linux/centos/7/source/edge
enabled=0
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg

[docker-ce-test]
name=Docker CE Test - $basearch
baseurl=https://download.docker.com/linux/centos/7/$basearch/test
enabled=0
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg

[docker-ce-test-debuginfo]
name=Docker CE Test - Debuginfo $basearch
baseurl=https://download.docker.com/linux/centos/7/debug-$basearch/test
enabled=0
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg

[docker-ce-test-source]
name=Docker CE Test - Sources
baseurl=https://download.docker.com/linux/centos/7/source/test
enabled=0
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg

[docker-ce-nightly]
name=Docker CE Nightly - $basearch
baseurl=https://download.docker.com/linux/centos/7/$basearch/nightly
enabled=0
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg

[docker-ce-nightly-debuginfo]
name=Docker CE Nightly - Debuginfo $basearch
baseurl=https://download.docker.com/linux/centos/7/debug-$basearch/nightly
enabled=0
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg

[docker-ce-nightly-source]
name=Docker CE Nightly - Sources
baseurl=https://download.docker.com/linux/centos/7/source/nightly
enabled=0
gpgcheck=1
gpgkey=https://download.docker.com/linux/centos/gpg

如果国外下载速度太慢,可以换做阿里云的yum仓库:

1
2
3
4
5
6
7
# vim /etc/yum.repos.d/docker-ce.repo 
[docker-ce-stable]
name=Docker CE Stable - $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/$basearch/stable
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

查看docker-ce的版本,目前docker19.0.3支持NVIDIA GPU

1
yum list docker-ce --showduplicates | sort -r

输出信息如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
docker-ce.x86_64            3:19.03.5-3.el7                    docker-ce-stable 
docker-ce.x86_64 3:19.03.5-3.el7 @docker-ce-stable
docker-ce.x86_64 3:19.03.4-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.3-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.2-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.1-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.0-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.9-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.8-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.7-3.el7 docker-ce-stable
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

启用ngihtlytest以及edge仓库是可选的。这些仓库包含在上面设置的 docker.repo 中,但默认情况下处于禁用状态。

1
2
3
sudo yum-config-manager --disable docker-ce-nightly
yum-config-manager --disable docker-ce-edge
yum-config-manager --disable docker-ce-test

安装最新版本的Docker Engine-Community和containerd,或者转到下一步安装特定版本:

1
yum install docker-ce docker-ce-cli

安装过程中,我们会发现docker需要依赖下面这些软件包:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Dependencies Resolved

=============================================================================================================
Package Arch Version Repository Size
=============================================================================================================
Installing:
docker-ce x86_64 3:19.03.5-3.el7 docker-ce-stable 24 M
Installing for dependencies:
audit-libs-python x86_64 2.8.5-4.el7 base 76 k
checkpolicy x86_64 2.5-8.el7 base 295 k
container-selinux noarch 2:2.107-3.el7 extras 39 k
containerd.io x86_64 1.2.10-3.2.el7 docker-ce-stable 23 M
docker-ce-cli x86_64 1:19.03.5-3.el7 docker-ce-stable 39 M
libcgroup x86_64 0.41-21.el7 base 66 k
libsemanage-python x86_64 2.5-14.el7 base 113 k
policycoreutils-python x86_64 2.5-33.el7 base 457 k
python-IPy noarch 0.75-6.el7 base 32 k
setools-libs x86_64 3.3.8-4.el7 base 620 k
Updating for dependencies:
audit x86_64 2.8.5-4.el7 base 256 k
audit-libs x86_64 2.8.5-4.el7 base 102 k
libsemanage x86_64 2.5-14.el7 base 151 k
policycoreutils x86_64 2.5-33.el7 base 916 k
selinux-policy noarch 3.13.1-252.el7.1 updates 492 k
selinux-policy-targeted noarch 3.13.1-252.el7.1 updates 7.0 M

Transaction Summary
=============================================================================================================
Install 1 Package (+10 Dependent packages)
Upgrade ( 6 Dependent packages)

Total download size: 98 M

如果想要安装 docker 最新稳定版,我们可以通过指定版本号的方式安装,查看版本号可以看看前面的 yum list 命令:

1
2
3
# 可以这样选择版本,我们选择 19.03.5 版本:
# yum install docker-ce-19.03.5 docker-ce-cli-19.03.5
yum install docker-ce-<DOCKER-VERSION> docker-ce-cli-<DOCKER-VERSION> containerd.io

2. 启动docker daemon和配置开机自动启动:

1
2
systemctl start docker
systemctl enable docker

3. Check一下安装的docker-ce版本号:

1
2
docker --version
Docker version 18.09.1, build 4c52b90

docker 代理设置

创建目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
sudo mkdir -p /etc/systemd/system/docker.service.d
```

##创建文件

- http 代理

``` bash
sudo touch /etc/systemd/system/docker.service.d/http-proxy.conf
```

配置内容如下:

``` vim
[Service]
Environment="HTTP_PROXY=http://10.57.22.219:3128/" "NO_PROXY=localhost,127.0.0.1,docker-registry.example.com,.corp"
  • https 代理
1
2
3
4
5
6
7
8
sudo touch /etc/systemd/system/docker.service.d/http-proxy.conf
```

配置内容如下:

``` vim
[Service]
Environment="HTTPS_PROXY=http://10.57.22.219:3128/" "NO_PROXY=localhost,127.0.0.1,docker-registry.example.com,.corp"

重启 docker

1
2
sudo systemctl daemon-reload
sudo systemctl restart docker

查看修改结果

1
2
3
4
5
# 执行西面命令
sudo systemctl show --property=Environment docker

# 输出的结果
Environment=HTTPS_PROXY=xxx.xxx.xxx.xxx:2181 NO_PROXY=localhost,127.0.0.1,mydocker-registry.com:5000

验证docker代理是否设置成功

设置成功后执行docker search 镜像名 就可以看到可以下载的镜像。
运行 hello-word 试一下:

1
docker run hello-world

如过配置成功,我们会看到下面的信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:4df8ca8a7e309c256d60d7971ea14c27672fc0d10c5f303856d7bc48f8cc17ff
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/

参考