Create file /etc/systemd/system/[email protected]
. SystemD calling binaries using an absolute path. In my case is prefixed by /usr/local/bin
, you should use paths specific for your environment.
[Unit]
Description=%i service with docker compose
PartOf=docker.service
After=docker.service
[Service]
Type=oneshot
RemainAfterExit=true
WorkingDirectory=/etc/docker/compose/%i
ExecStart=/usr/local/bin/docker-compose up -d --remove-orphans
ExecStop=/usr/local/bin/docker-compose down
[Install]
WantedBy=multi-user.target
Place your docker-compose.yml
into /etc/docker/compose/myservice
and call
systemctl start docker-compose@myservice
Create /etc/systemd/system/docker-cleanup.timer
with this content:
[Unit]
Description=Docker cleanup timer
[Timer]
OnUnitInactiveSec=12h
[Install]
WantedBy=timers.target
And service file /etc/systemd/system/docker-cleanup.service
:
[Unit]
Description=Docker cleanup
Requires=docker.service
After=docker.service
[Service]
Type=oneshot
WorkingDirectory=/tmp
User=root
Group=root
ExecStart=/usr/bin/docker system prune -af
[Install]
WantedBy=multi-user.target
run systemctl enable docker-cleanup.timer
for enabling the timer
Just add the following line to the /etc/docker/daemon.json
:
{
...
"log-driver": "journald",
...
}
And restart your docker service.
I'm still new to docker and learning daily. I really like this configuration but I'm running into an issue.
My setup: I have created docker-compose.yml file for each of my docker container services
e.g.
/mnt/dockerdata/portainer/compose/docker-compose.yml
/etc/syustemd/system/[email protected]
My issue is with the --remove-orphans switch. When you start the systemd service it kills the other services. e.g. If I run
systemctl start [email protected]
I see "Removing orphan container "portainer"" in the logs for the bitwarden service and portainer and nextcloud are not running or vise-versa.MY QUESTION: I want to clean up any orphan containers, but I don't want it to effect my other services. What would be and effective way of this within the systemd service file?
docker ps shows that only bitwarden is running
next I run docker-compose up in the portainer/compose directory. You can see that docker compose up is saying that the bitwarden container is an orphan container which it isn't.
docker ps shows that only portainer is running
when you run docker-compose up -d --remove-orphans within the bitwarden/compose directory it removes the portainer container.