Summary
If I have a systemd template unit foo@.service
, how can I symlink to or create an alias for a specific template instance, such that, for example, bar.service
would be a shortcut for foo@bar.service
?
Background
I have a couple of services run by docker-compose.
/var/docker-compose/nginx/docker-compose.yml
/var/docker-compose/nextcloud/docker-compose.yml
/var/docker-compose/diaspora/docker-compose.yml
To manage these services with systemd, I've adopted an approach to use template units in order to avoid duplication as all the docker-compose unit files would almost be the same.
# /etc/systemd/system/docker-compose@.service
[Unit]
Description=%i service with docker compose
Requires=docker.service
After=docker.service
[Service]
Restart=always
WorkingDirectory=/var/docker-compose/%i
# Remove old containers, images and volumes
ExecStartPre=/usr/bin/docker-compose down -v
ExecStartPre=/usr/bin/docker-compose rm -fv
ExecStartPre=-/bin/bash -c 'docker volume ls -qf "name=%i_" | xargs docker volume rm'
ExecStartPre=-/bin/bash -c 'docker network ls -qf "name=%i_" | xargs docker network rm'
ExecStartPre=-/bin/bash -c 'docker ps -aqf "name=%i_*" | xargs docker rm'
# Compose up
ExecStart=/usr/bin/docker-compose up %i
# Compose down, remove containers and volumes
ExecStop=/usr/bin/docker-compose down -v
[Install]
WantedBy=multi-user.target
Goal
Now I would like to have aliases or symlinks to the individual services to make starting and stopping them a little shorter.
Rather than
systemctl stop docker-compose@nginx
I would like to type:
systemctl stop nginx
What does not work
I've tried to create a symlink:
cd /etc/systemd/system ln -s docker-compose@nginx.service nginx.service
but
nginx.service
is not found when I want to start or stop it.I've tried to define an alias in the template unit:
[Install] Alias=%i.service
but, if I understand this correctly, this is not allowed and it did not work for me.
I haven't found a way to "import" or "require" a base unit file and overwrite just the
WorkingDirectory
line. Then, I could create separate, short files like this:# /etc/systemd/system/nginx.service Import=docker-compose@.service WorkingDirectory=/var/docker-compose/nginx
Thanks!
Alias
line, you need to updatesystemd
viasystemctl daemon-reload
and then you need re-enable the service.