I’ve set the BIOS on the new server to resume running after a power failure. The OS happily comes back no problem, and docker even starts up – but none of the containers.
Apparently in docker 1.2 there is now support to restart your containers after boot, this sounds useful, but the version of docker in Ubuntu 16.04 is 1.12.1, close but not quite recent enough.
No worries, we can use systemd to sort this out. The docker documentation has some examples, but I found an article which takes a slightly different approach – one they claim is more aligned to CoreOS.
Adding our own services to systemd is simply a matter of adding a file to /etc/systemd/system/. The very first one I want to add is one to host my own registry. Now you probably don’t want to host your own registry since you could simply use the public dockerhub, or any number of other solutions. However, I’m also the type of person who hosts their own email, so of course I’m going to host my own registry.
Description=Private Docker Registry
ExecStartPre=-/usr/bin/docker stop %n
ExecStartPre=-/usr/bin/docker rm %n
ExecStartPre=/usr/bin/docker pull registry
ExecStart=/usr/bin/docker run -p 5000:5000 -v /var/lib/registry:/var/lib/registry --name %n registry
The %n in the file is replaced with the ‘full unit name’. If we save this file as /etc/systemd/system/docker-registry.service the unit name should be docker-registry.
In a simple configuration, the local docker registry will persist data as a docker volume. I decided it was safer to map to a local file tree.
Once you’ve created the file /etc/systemd/system/docker-registry.service we need to reload the systemd daemon and start the service.
$ sudo systemctl daemon-reload
$ sudo systemctl start docker-registry.service
If all has gone well,
docker ps should show us the new running registry (it does). Now let’s make it start every boot.
$ sudo systemctl enable docker-registry.service
And yup, a reboot and we see the registry has started. So now we have a local registry to store our containers in, and a pattern to apply to make them services that start even after a reboot (expected or unexpected).
Pushing images to a local registry is covered in the documentation. I might later want to add a certificate to the registry, but it is not required.