With Ubuntu 15.04 switching from Upstart to systemd, many developers need to adapt their service management scripts. The docker-compose integration becomes particularly important when you need proper process supervision and restart capabilities.
Here's an improved version of your systemd service file that includes additional best practices:
[Unit]
Description=My Server Container
Documentation=https://docs.docker.com/compose/
Requires=docker.service
After=docker.service network-online.target
Wants=network-online.target
[Service]
Type=forking
Restart=always
RestartSec=30
WorkingDirectory=/projects/my_server
ExecStartPre=/bin/sh -c 'while [ ! -f /projects/my_server/docker-compose.yml ]; do sleep 1; done'
ExecStart=/usr/local/bin/docker-compose up -d
ExecStop=/usr/local/bin/docker-compose stop
ExecReload=/usr/local/bin/docker-compose restart
TimeoutStartSec=0
[Install]
WantedBy=multi-user.target
The enhanced service file includes several important features:
Type=forking
properly handles the daemon behavior of docker-composeRestartSec
prevents immediate restart loopsWorkingDirectory
simplifies command execution- Added network dependency for better startup ordering
- Included reload capability for configuration changes
For better container monitoring, consider this alternative implementation that tracks the main process:
[Service]
Type=simple
ExecStart=/bin/sh -c '/usr/local/bin/docker-compose up 2>&1 | logger -t my_server'
ExecStop=/usr/local/bin/docker-compose stop
Restart=always
RestartSec=10
TimeoutStopSec=5
KillMode=mixed
To implement and control your service:
# Place the service file
sudo cp my_server.service /etc/systemd/system/
# Enable and start the service
sudo systemctl daemon-reload
sudo systemctl enable my_server.service
sudo systemctl start my_server.service
# Check status
sudo systemctl status my_server.service -l
# View logs
journalctl -u my_server.service -f
If you encounter problems:
- Check container logs:
docker-compose logs
- Verify file permissions in /projects/my_server
- Ensure docker.socket is active:
systemctl status docker.socket
- Test docker-compose manually before systemd integration
With Ubuntu 15.04's switch to systemd, many service management approaches need rethinking. Docker-compose projects previously managed through Upstart (as shown in the example) require adaptation to systemd's service management paradigm.
Here's a robust systemd service file that handles docker-compose lifecycle management:
[Unit]
Description=My Server Container Stack
Requires=docker.service
After=docker.service network.target
[Service]
Type=oneshot
RemainAfterExit=yes
WorkingDirectory=/projects/my_server
ExecStart=/usr/local/bin/docker-compose -f docker-compose.yml up -d
ExecStop=/usr/local/bin/docker-compose -f docker-compose.yml down
ExecReload=/usr/local/bin/docker-compose -f docker-compose.yml restart
# Important for container monitoring
RestartSec=5s
Restart=on-failure
[Install]
WantedBy=multi-user.target
For more sophisticated control, consider these additions:
# Add to [Service] section
EnvironmentFile=/etc/default/my_server_compose
TimeoutStartSec=300
TimeoutStopSec=120
To implement the filesystem check from the original Upstart script:
ExecStartPre=/bin/bash -c 'while [ ! -f /projects/my_server/docker-compose.yml ]; do sleep 1; done'
For continuous monitoring of your container status:
[Service]
...
ExecStartPost=/bin/bash -c 'while docker ps | grep "my_server"; do sleep 30; done'
After creating your service file at /etc/systemd/system/my_server.service
:
sudo systemctl daemon-reload
sudo systemctl enable my_server.service
sudo systemctl start my_server.service
Use these commands to verify service status:
journalctl -u my_server.service -f
systemctl status my_server.service
docker-compose -f /projects/my_server/docker-compose.yml ps