Start Docker Compose on Boot
You want your Docker Compose stack to start automatically if your server reboots. There are two main ways to do this.
Method 1: Restart Policies (Easiest)
The simplest way is to let the Docker daemon handle it. Add restart: always or restart: unless-stopped to every service in your docker-compose.yml.
services:
web:
image: nginx
restart: unless-stopped
db:
image: postgres
restart: unless-stopped
always: Always restart the container if it stops.unless-stopped: Restart the container unless it was arbitrarily stopped (bydocker stop). This is the recommended default.
How it works: When the Docker daemon starts (on boot), it checks for containers with these policies and starts them. You don't need a separate systemd service for the stack, just for the Docker engine itself (which is enabled by default on most Linux distros).
Method 2: Systemd Service (Advanced)
If you need more control (e.g., dependencies on other system services, or you want to manage the whole stack as a unit), you can create a systemd unit.
- Create a service file:
/etc/systemd/system/my-app.service
[Unit]
Description=My Docker Compose App
Requires=docker.service
After=docker.service
[Service]
Type=oneshot
RemainAfterExit=yes
WorkingDirectory=/path/to/your/project
ExecStart=/usr/bin/docker compose up -d --remove-orphans
ExecStop=/usr/bin/docker compose down
[Install]
WantedBy=multi-user.target
- Enable and start it:
sudo systemctl enable my-app
sudo systemctl start my-app
Which one to use?
- Use Restart Policies for 99% of cases. It's portable (part of the YAML) and simple.
- Use Systemd only if you need to ensure the entire stack is brought up/down together in a specific way relative to the host OS.
Forget about server maintenance
On Hostim.dev, your apps are managed by our robust infrastructure. We handle restarts, hardware failures, and uptime.