Docker容器自动重启

Docker

一.场景

因docker某实例经常无缘无故断开(docker run 没有加–restart=always),需要手动重启,不方便,故需要设置docker自动重启。 
建立一个docker实例

二.自定义系统服务

在目录/etc/systemd/system中添加文件docker-db2.service

文件内容如下

[Unit]
Description=centos_db2 container
Requires=docker.service
After=docker.service
[Service]
Restart=always
ExecStart=/usr/bin/docker start db2
ExecStop=/usr/bin/docker stop db2
[Install]
WantedBy=default.target

三.设置开机启动

systemctl enable docker-db2.service
systemctl start docker-db2.service
systemctl status docker-db2.service

四.备注

现在用docker stop db2 不能关闭该容器,必须用

service docker-db2.service  stop

才能停止

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment