我有一个docker-compose项目,我想通过systemd控制.在upstart中,我会使用如下所示的脚本:
description "Start/Stop server" author "Jim Cortez" start on filesystem and started docker stop on runlevel [!2345] respawn limit 3 240 pre-start script # wait (if necessary) for our docker context to be accessible while [ ! -f /projects/my_server/docker-compose.yml ] do sleep 1 done /usr/local/bin/docker-compose -f /projects/my_server/docker-compose.yml up -d end script script sleepWhileAppIsUp(){ while docker ps | grep "$1" >/dev/null; do sleep 2 done } sleepWhileAppIsUp "my_server" end script # stop docker container after the stop event has completed post-stop script if docker ps | grep my_server; then /usr/local/bin/docker-compose -f /projects/my_server/docker-compose.yml stop fi end script
(以上改编自here)
但是,我现在在运行Ubuntu 15.04的docker主机上运行,该主机已切换到systemd.如何将上述内容作为systemd服务脚本?只需启动docker-compose守护程序,就不允许systemd在发生故障时进行跟踪和重启.
这是我到目前为止:
[Unit] Description=My Server container Requires=docker.service After=docker.service [Service] Restart=always ExecStart=/usr/local/bin/docker-compose -f /projects/my_server/docker-compose.yml up -d ExecStop=/usr/local/bin/docker-compose -f /projects/my_server/docker-compose.yml stop [Install] WantedBy=local.target