很多时候需要实现平台服务在物理机宕机重启或服务器启动的时候,将平台服务自动拉起的能力,此时可以考虑将docker服务配置为系统服务,通过系统服务自启动能力将平台服务自动拉起。除此之外,也有一些运营管理场景需要快速达到服务一键管理的能力,也需要将docker服务配置为系统服务更好满足服务一键管理要求。因此整理了一种将docker服务转为系统服务的操作方法,以下是具体方法介绍。
首先,进入docker服务所在机器,执行“ps-ef | grep docker”,将demon.json进程手动杀掉。其次,手工将docker进程停掉(Kill -9 pid ),进程例子:dockerd --config-file ./daemon.json。然后,修改service 文件中ExecStart的值为/usr/bin/dockerd --config-file + daemon.json文件的绝对路径。daemon.json文件的路径为云平台服务所在机器(在XXX/pacakge文件夹下的daemon.json文件的绝对路径。示例ExecStart=/usr/bin/dockerd --config-file /home/XXX/package/daemon.json 。请按实际部署情况修改这个绝对路径。)最后,将service和docker.socket文件拷贝至 /etc/systemd/system目录下。并依次执行“sudo systemctl daemon-reload”(重启systemctl 守护进程),sudo systemctl start docker(启动docker服务),systemctl enable docker (开启docker服务开机自启动),sudo chmod 777 /var/run/docker.sock
上述操作完成后,可通过以下命令管理docker服务
sudo systemctl stop docker (暂停docker 服务)
sudo systemctl start docker (启动docker服务)
sudo systemctl restart docker(重启docker服务)
每次使用上述命令启动或重启docker服务后,需再次执行“sudo chmod 777 /var/run/docker.sock”命令。以下是docker.server和docker.sock具体文件内容。
附录:docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target
[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
ExecReload=/bin/kill -s HUP $MAINPID
# 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
# 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
附录:docker.sock
[Unit]
Description=Docker Socket for the API
PartOf=docker.service
[Socket]
ListenStream=/var/run/docker.sock
SocketMode=100
SocketUser=root
SocketGroup=docker
[Install]
WantedBy=sockets.target