说明
本文只讲述docker离线安装及清理的步骤,关于docker本身的含义、技术特性等不再描述。
根据官网的介绍,在Linux系统中安装docker,可通过在线安装(centos的yum,ubuntu的apt-get等),离线rpm包安装,二进制文件安装等。基于一些环境因素的限制,不能通过在线安装,或rpm离线安装包依赖的问题,二进制文件安装docker显得简单且快速,本文介绍通过二进制文件安装docker。
离线包下载
在官网选择具体的版本,比如本文选择docker-18.09.5版本
mkdir -p ~/tools/docker
cd ~/tools/docker
wget https://download.docker.com/linux/static/stable/x86_64/docker-18.09.5.tgz
用户组配置
# 新建用户组
sudo groupadd docker
# 将登录用户加入到docker用户组中
sudo usermod -aG docker $USER
# 更新用户组
sudo newgrp docker
安装包解压
cd ~/tools/docker
mkdir install-package
# 用于清理
tar -xf docker-18.09.5.tgz -C install-package --strip-components=1
# 解压安装至/usr/local/bin目录下
sudo tar -xf docker-18.09.5.tgz -C /usr/local/bin --strip-components=1
配置docker服务
# 修改docker默认存储路径
sudo mkdir -p ~/data/docker
配置docker.service
在Linux系统配置systemctl管理的服务信息:参考官网文档 。
配置/etc/systemd/system/docker.service
文件后,需要执行systemctl daemon-reload
重新加载后生效。
sudo cat > /etc/systemd/system/docker.service << EOF
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
#BindsTo=containerd.service
After=network-online.target firewalld.service #containerd.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的启动可选参数,可通过dockerd --help查看
ExecStart=/usr/local/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always
# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
# Both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
StartLimitBurst=3
# Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
# Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
# this option work for either version of systemd.
StartLimitInterval=60s
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Comment TasksMax if your systemd version does not supports it.
# Only systemd 226 and above support this option.
TasksMax=infinity
# 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
[Install]
WantedBy=multi-user.target
EOF
配置daemon.json(可选)
daemon.json是docker.service中ExecStart启动参数的补充,以json格式定义,可通过/usr/local/bin/dockerd --help查看所有的参数
sudo cat >/etc/docker/daemon.json << EOF
{
"storage-driver": "overlay2",
"log-driver":"json-file",
"log-opts":{
"max-size":"50m",
"max-file":"2"
},
"hosts": ["unix:///var/run/docker.sock"]
}
EOF
启动docker服务
# 因为在/etc/systemd/system/docker.service已经配置了docker.service
# 所以只需重新加载docker服务的配置文件
# 参考关于systemd命令的资料https://blog.51cto.com/xuding/1730952
sudo systemctl daemon-reload
# 通过systemctl启动docker服务
sudo systemctl start docker
# 设置为开机启动
sudo systemctl enable docker
# 通过可执行文件dockerd启动docker服务
# sudo dockerd \
# --exec-root=/var/run/docker \
# --data-root=/home/ubuntu/data/docker \
# &
# 验证开放2375端口
sudo netstat -lntp | grep dockerd
彻底清理docker
# 本文只针对于通过解压安装包的方式进行docker的清理和卸载
# 停止docker服务
sudo systemctl stop docker
# 杀死docker进程(为防止特殊情况下有残留的docker进程)
ps -ef | grep docker | awk '{print $2}' | xargs sudo kill -s 9
# 删除持久化Docker状态的根目录
# 通过docker info |grep 'Docker Root Dir:'命令查看
# 默认值/var/lib/docker,可通过dockerd命令的--data-root选项修改
sudo rm -rf /home/ubuntu/data/docker
# 删除执行状态文件的根目录
# 默认值/var/run/docker,可通过dockerd命令的--exec-root选项修改
sudo rm -rf /var/run/docker
# 删除默认监听的Unix域套接字,容器中的进程可以通过它与Docker守护进程进行通信
sudo rm -rf /var/run/docker.sock
# 删除docker守护进程PID文件
# 默认值/var/run/docker.pid,可通过dockerd命令的-p或--pidfile选项修改
sudo rm -rf /var/run/docker.pid
# 删除系统服务配置文件
sudo rm -rf /etc/systemd/system/docker.service
# 删除docker配置文件
# 默认值/etc/docker/daemon.json,可通过dockerd命令的--config-file选项修改
sudo rm -rf /etc/docker/daemon.json
# 删除自动生成的文件,tls相关配置:~/.docker/{ca.pem,cert.pem,key.pem}
sudo rm -rf ~/.docker/
# 清理可执行文件
ls ~/tools/docker/install-package |awk '{print "/usr/local/bin/"$1}'|xargs sudo rm -rf