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.targetPlace 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.targetAnd 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.targetrun 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.
@parisni It looks great, but this is not applicable to docker-compose. This is the python script that establishes a connection with the docker daemon, and executes everything that has been described in YAML through the Docker API. Docker, in turn, executes commands from the root user, and all the above restrictions will not be applied.