二进制文件安装Docker指南
# 二进制文件安装 Docker 指南
郭群 于 2020 年 07 月 16 日
# 引言
yum 命令使用 rpm 包离线安装,各种缺少依赖,很是烦躁。 猛然发现官网还提供了二进制安装包,盘他。
# 环境准备
# 一、系统服务版本依赖检查
# (一)Linux 内核版本
Docker 官网要求内核版本>=3.10,检查一下:
[root@localhost docker-18.09.4-binary]# uname -a
Linux localhost.localdomain 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
2
# (二)iptables 版本
Docker 官网要求iptables版本>=1.4,检查一下:
[root@localhost docker-18.09.4-binary]# iptables -V
iptables v1.4.21
2
# (三)git 版本
Docker 官网要求git版本>=1.7,检查一下:
[root@localhost docker-18.09.4-binary]# git --version
git version 1.8.3.1
2
# (四)ps 命令已安装
[root@localhost docker-18.09.4-binary]# ps
PID TTY TIME CMD
20375 pts/0 00:00:00 ps
50036 pts/0 00:00:00 bash
2
3
4
# (五)XZ Utils
略
# (六)cgroupfs
略
# 二、环境安全检查
# (一)操作系统
开启 selinux,略,可以先不做。
# (二)Docker 守护进程相关
# 启用 seccomp 配置
检查方式
[root@localhost docker-18.09.4-binary]# grep CONFIG_SECCOMP= /boot/config-$(uname -r)
CONFIG_SECCOMP=y
2
# 启用用户命名空间
略,可以先不做
# 安装静态二进制文件
# 一、下载所需的压缩包
此处准备的安装包是docker-18.09.4版本
下载地址: docker-18.09.4 (opens new window)
MD5: 17a327d2871874892b3b3d7532d7975c
# 二、解压
解压到任意目录了
[root@localhost docker-18.09.4-binary]# ls
docker-18.09.4.tgz
[root@localhost docker-18.09.4-binary]# tar zxvf docker-18.09.4.tgz
docker/
docker/ctr
docker/containerd-shim
docker/dockerd
docker/docker-proxy
docker/runc
docker/containerd
docker/docker-init
docker/docker
2
3
4
5
6
7
8
9
10
11
12
# 三、拷贝到/usr/bin目录
[root@localhost docker-18.09.4-binary]# sudo cp docker/* /usr/bin/
# 四、启动 Docker 守护进程
[root@localhost ~]# sudo dockerd &
[1] 23943
[root@localhost ~]# INFO[2020-07-16T08:18:34.530770919+08:00] libcontainerd: started new containerd process pid=23972
INFO[2020-07-16T08:18:34.531043082+08:00] parsed scheme: "unix" module=grpc
INFO[2020-07-16T08:18:34.531073503+08:00] scheme "unix" not registered, fallback to default scheme module=grpc
INFO[2020-07-16T08:18:34.531168709+08:00] ccResolverWrapper: sending new addresses to cc: [{unix:///var/run/docker/containerd/containerd.sock 0 <nil>}] module=grpc
INFO[2020-07-16T08:18:34.531203331+08:00] ClientConn switching balancer to "pick_first" module=grpc
INFO[2020-07-16T08:18:34.531344189+08:00] pickfirstBalancer: HandleSubConnStateChange: 0xc420a5e670, CONNECTING module=grpc
INFO[2020-07-16T08:18:34.565914298+08:00] starting containerd revision=bb71b10fd8f58240ca47fbb579b9d1028eea7c84 version=v1.2.5
......
INFO[2020-07-16T08:18:34.674048937+08:00] Loading containers: start.
INFO[2020-07-16T08:18:34.892551101+08:00] 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
INFO[2020-07-16T08:18:35.017834592+08:00] Loading containers: done.
INFO[2020-07-16T08:18:35.061314596+08:00] Docker daemon commit=d14af54 graphdriver(s)=overlay2 version=18.09.4
INFO[2020-07-16T08:18:35.061450799+08:00] Daemon has completed initialization
INFO[2020-07-16T08:18:35.093922279+08:00] API listen on /var/run/docker.sock
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
新开一个命令行窗口,测试下 docker 是否可用
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2
3
4
完美~
# 五、修改 Docker 服务默认网桥 IP 网段
在上面的日志中可以看到,docker 默认网桥的 IP 网段是 172.17.0.0/16,经常和我们的服务器 IP 网段冲突,
因此需要修改/etc/docker/daemon.json,若不存在此文件则新建即可:
{
"bip": "172.31.0.1/16"
}
2
3
修改后重启 dockerd,检查是否修改成功
[root@localhost ~]# ip addr | grep docker0 -A 4
6: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN
link/ether 02:42:1c:db:a3:c0 brd ff:ff:ff:ff:ff:ff
inet 172.31.0.1/16 brd 172.31.255.255 scope global docker0
valid_lft forever preferred_lft forever
2
3
4
5
完美,至此静态文件安装 Docker 服务已经成功。
# docker 注册为 service
虽然 docker 服务已经可以使用了,但终究不能像 yum 命令安装的那样通过systemctl命令管理控制,而且不能开机自启动。
我们还要再做个收尾工作,将 docker 注册为 service,相关知识的详情参见《如何把应用服务注册到 systemd》
此处给出配置文件和操作说明
# STEP-1 新建配置
# docker.service
新建/usr/lib/systemd/system/docker.service文件,写入如下内容
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target docker.socket firewalld.service
Wants=network-online.target
Requires=docker.socket
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H fd://
ExecReload=/bin/kill -s HUP $MAINPID
LimitNOFILE=1048576
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNPROC=infinity
LimitCORE=infinity
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
TasksMax=infinity
TimeoutStartSec=0
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
# restart the docker process if it exits prematurely
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
[Install]
WantedBy=multi-user.target
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
# docker.socket
新建/lib/systemd/system/docker.socket文件,写入如下内容
/usr/lib/systemd/system/docker.service
[Unit]
Description=Docker Socket for the API
PartOf=docker.service
[Socket]
ListenStream=/var/run/docker.sock
SocketMode=0660
SocketUser=root
SocketGroup=docker
[Install]
WantedBy=sockets.target
2
3
4
5
6
7
8
9
10
11
12
# STEP-2 杀掉 dockerd 进程
[root@localhost ~]# ps -ef | grep docker
root 26997 50036 0 08:26 pts/0 00:00:00 sudo dockerd
root 26998 26997 0 08:26 pts/0 00:00:01 dockerd
root 27028 26998 0 08:26 ? 00:00:00 containerd --config /var/run/docker/containerd/containerd.toml --log-level info
root 30175 50036 0 08:35 pts/0 00:00:00 grep --color=auto docker
[root@localhost ~]# kill -9 26997 26998 27028
[root@localhost ~]# ps -ef | grep docker
2
3
4
5
6
7
# STEP-3 重新加载配置
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]#
2
# STEP-4 将 Docker 设置为开机启动
[root@localhost ~]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
2
# STEP-5 启动 Docker 服务
[root@localhost ~]# systemctl start docker
# STEP-6 检查测试 docker 命令
[root@localhost ~]# docker version
Client: Docker Engine - Community
Version: 18.09.4
API version: 1.39
Go version: go1.10.8
Git commit: d14af54
Built: Wed Mar 27 18:33:40 2019
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 18.09.4
API version: 1.39 (minimum version 1.12)
Go version: go1.10.8
Git commit: d14af54
Built: Wed Mar 27 18:41:10 2019
OS/Arch: linux/amd64
Experimental: false
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
圆满了,完美了!