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)