Create docker-compose service
Example of systemd service configuration with a docker-compose.yml file in the folder /opt/karnak (If it’s another directory you have to adapt the script).
By default, Docker needs root privileges to be executed.
Manage Docker as a non-root user
For more details, the following commands are inspired by the official Docker documentation.
-
Create the
docker
group.sudo groupadd docker
-
Add your user to the
docker
group.sudo usermod -aG docker $USER
-
Activate the changes to groups.
newgrp docker
-
Verify that you can run
docker
commands withoutsudo
.docker run hello-world
Specify User in the service
In the [Service]
section of the karnak.service (see below), it’s possible to specify the user who will run the service.
User=root
Create the service
Instructions:
- Go to /etc/systemd/system
- Create the file ( eg: $ sudo touch karnak.service )
- Copy and paste the config below (eg: $ sudo nano karnak.service):
# /etc/systemd/system/karnak.service
#########################
# KARNAK #
# SERVICE #
##########################
[Unit]
Description=Docker Compose KARNAK Service
Requires=docker.service
After=docker.service network.target
[Service]
Type=oneshot
RemainAfterExit=yes
WorkingDirectory=/opt/karnak
ExecStart=/usr/local/bin/docker-compose up -d
ExecStop=/usr/local/bin/docker-compose down
TimeoutStartSec=0
[Install]
WantedBy=multi-user.target
Test the service:
- $ systemctl start karnak.service
- $ systemctl status karnak.service
- $ systemctl enable karnak.service