I'm trying to set up a Docker Compose service to start automatically using Systemd. The service starts, but auto-completion does not work, although it does when I run Docker Compose manually. Here's the status output and the Systemd service file:
docker-compose@tabby.service - tabby service with docker compose
Loaded: loaded (/etc/systemd/system/docker-compose@tabby.service; enabled; preset: disabled)
Active: active (running) since Fri 2024-04-12 14:28:57 EDT; 2s ago
Process: 3665 ExecStartPre=/usr/bin/docker compose pull (code=exited, status=0/SUCCESS)
Main PID: 3707 (docker)
Tasks: 27 (limit: 38141)
Memory: 37.3M (peak: 38.8M)
CPU: 111ms
CGroup: /system.slice/system-docker\x2dcompose.slice/docker-compose@tabby.service
docker compose up --remove-orphans
docker-compose compose up --remove-orphans
### /etc/systemd/system/docker-compose@tabby.service
[Unit]
Description=%i service with docker compose
Requires=docker.service
After=docker.service
[Service]
WorkingDirectory=/opt/%i
ExecStartPre=-/usr/bin/docker compose pull
ExecStart=/usr/bin/docker compose up --remove-orphans
ExecStop=/usr/bin/docker compose down
ExecReload=/usr/bin/docker compose pull
ExecReload=/usr/bin/docker compose up --remove-orphans
[Install]
WantedBy=multi-user.target
I suspect it might be related to the HOME
variable not being set, as indicated in the status output. How can I resolve this issue?
Etienne
Asked on Apr 12, 2024
The problem seems to be related to the HOME
environment variable not being set when the Systemd service runs. Since Systemd services run as root by default, they don't have a user's HOME
environment variable set. To fix this, you can hard-code the data directory in your Docker Compose file or pass the HOME
variable to the service. Here's how you can modify the Systemd service file to include the HOME
variable:
[Service]
Environment=HOME=/opt/tabby
WorkingDirectory=/opt/%i
ExecStartPre=-/usr/bin/docker compose pull
ExecStart=/usr/bin/docker compose up --remove-orphans
ExecStop=/usr/bin/docker compose down
ExecReload=/usr/bin/docker compose pull
ExecReload=/usr/bin/docker compose up --remove-orphans
By setting Environment=HOME=/opt/tabby
, you ensure that the Docker Compose service has access to the necessary environment variable. Make sure to replace /opt/tabby
with the actual path you want to use as the HOME
directory for the service.