Score:2

How to run a docker-compose as a systemd service?

cn flag

I found this question, but that one uses cron - I'd like to use a systemd service instead. My docker-compose.yml is in /home/me, and I run it with sudo docker compose up (and just keep it running in the background with tmux), and stop it with sudo docker compose down.

While functional, I'd rather do this using a systemd service to start/stop my docker-compose.yml file.

How can I turn my docker-compose.yml file into a (working) systemd service?

Score:3
cn flag

I'll assume the docker-compose file is at /home/cocomac/docker-compose.yml, but you should replace it in my example with whatever the path to your docker compose file is.

First, let's create our service file. I like the text editor micro, personally, but feel free to use a different one. I'll call my service docker-compose.service, and it will be stored in /etc/systemd/system. Feel free to use a different name if you'd like, although if you do, remember to use that name when following the rest of this answer.

First, let's create and open the file with a text editor:

$ micro /etc/systemd/system/docker-compose.service

Put the following into the file:

[Unit]
Description=Some personal Docker containers
After=docker.service
Requires=docker.service

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/bash -c "docker compose -f /home/cocomac/docker-compose.yml up --detach"
ExecStop=/bin/bash -c "docker compose -f /home/cocomac/docker-compose.yml stop"

[Install]
WantedBy=multi-user.target

This gives our service a description, start + stop commands, as well as what it requires to be running before it starts. See this Arch Wiki page for an explanation of the different service types and what RemainAfterExit does.

Save the file and exit your text editor. You should now be able to do sudo systemctl start docker-compose to start your new systemd service. It may take a moment while it creates the containers, but once it finishes starting, you can do sudo systemctl status docker-compose to check the status of your service. You can also do sudo systemctl stop docker-compose to stop it (or sudo systemctl enable docker-compose to autostart it on boot)

Acceptable Name avatar
us flag
Given that the container runs from a non-root home directory you probably want `[Service] ... User=cocomac`
Score:0
fk flag

Using the following for the Service stanza also works & has the benefit that you can view logs using journalctl -u docker-compose:

[Service]
Type=simple
ExecStart=/bin/bash -c "docker-compose -f /home/cocomac/docker-compose.yml up"
ExecStop=/bin/bash -c "docker-compose -f /home/cocomac/docker-compose.yml stop"

Note that the docker-compose command does not include the --detach flag and you do not need the RemainAfterExit option. Note also that on the version of Ubuntu I tested this on (20.04), docker-compose is a separate command.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.