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
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
Now I would like to have aliases or symlinks to the individual services to make starting and stopping them a little shorter.
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 email@example.com nginx.service
nginx.serviceis not found when I want to start or stop it.
I've tried to define an alias in the template unit:
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
WorkingDirectoryline. Then, I could create separate, short files like this:
# /etc/systemd/system/nginx.service Import=docker-compose@.service WorkingDirectory=/var/docker-compose/nginx