Starting docker-compose with systemd on ubuntu 15.04

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

I suggest removing the -d option. You do not need to run the containers in the background in this case.


I found that: The author uses a similar approach as you http://trackless.ca/2015/12/21/docker-compose-meets-systemd/. But additionally he creates one systemd service for each docker-compose service.

You might consider to convert your docker-compose file to multiple systemd service files to get rid of the docker-compose dependency: http://container-transform.readthedocs.org/ I used that approach, it works fine for simple setups.