Published on

Ubuntu将docker容器设置成服务

Authors
  • avatar
    Name
    Lif
    Twitter

如果需要把容器设置成随系统启动自动启动,可以把容器的启动做成服务

sudo su -
cd /etc/systemd/system
touch docker-container.service
vim docker-container.service

写入如下:

[Unit]
Description=Docker Container

[Service]
Restart=always
ExecStart=/bin/bash -c "/usr/bin/docker start -a my-first-container && /usr/bin/docker start -a my-second-container"
ExecStop=/bin/bash -c "/usr/bin/docker stop -t 2 my-first-container && /usr/bin/docker stop -t 2 my-second-container"

[Install]
WantedBy=multi-user.target

然后应用服务

systemctl enable docker-container.service
systemctl start docker-container.service