I have a docker-compose project that I would like to control through systemd. In upstart, I would use a script that looks like this:
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
(above adapted from here)
However, I am now running on a docker host that runs Ubuntu 15.04, which has switched to systemd. How can I do the above as a systemd service script? Simply launching the docker-compose daemon will not allow systemd to track and restart in case of failure.
Here is what I have so far:
[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