我有一个想通过systemd控制的docker-compose项目。在暴发户中,我将使用如下所示的脚本:
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
(以上从这里改编)
但是,我现在在运行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