Deplying Docker using systemd

2019-10-28 1 min read

    A few months ago in a burst of inspiration I converted a bunch of my side projects to run inside Docker. Unfortunately, I didn’t do the follow up work of actually creating a Kubernetes cluster and instead came up with a ridiculously hacky process to get them running. I created a simple shell script that would just build and run a Docker image and have just been running it inside a screen session.

    This worked surprisingly well. The only problem was that every time the instance was rebooted the screen sessions died and I had to SSH into it in order to restart the scripts. Earlier today I had another instance reboot and decided to finally do something about it. The solution I ended up with was to just bundle the aforementioned shell script into a systemd file and configure it to run at startup. It’s been a few hours and so far so good and delay the inevitable move to Kubernetes by a few more months.

    run.sh
    (docker rm test || true) && docker build -t test:latest . && docker run --env-file env.list --name test -p 8001:8000 -i test:latest
    /etc/systemd/system/test.service
    [Unit]
    Description=Test service.
    
    [Service]
    #Type=simple
    WorkingDirectory=/root/test
    ExecStart=/bin/bash /root/test/run.sh
    Restart=always
    RestartSec=10
    
    [Install]
    WantedBy=multi-user.target