Start docker container at boot using systemd

To register a new service on Systemd create file /etc/systemd/system/myapp.service with the following content.


ExecStart=/usr/bin/docker run --name ping --rm -e -e TIMEOUT=5 willfarrell/ping
ExecStop=/usr/bin/docker stop ping


Where we indicate that myapp requires docker to start. What this container is doing is simply pinging google every 5 seconds.

Now to start the service:

sudo systemctl start myapp

And to see if it is really working:

docker ps
docker logs -f ping

If you want you can ask systemd to stop the service via:

sudo systemctl stop myapp

The final trick, is enable myapp to run every time the system is booted:

sudo systemctl enable myapp.service