Docker actually has built-in functionality for container auto-start through restart policies. When creating or running a container, you can specify one of these policies:
docker run -d --restart unless-stopped my_container_image
Available restart policies:
- no: Do not automatically restart (default)
- on-failure: Restart if exits with non-zero status
- always: Always restart
- unless-stopped: Always restart unless explicitly stopped
For more control, creating systemd service files is recommended:
# /etc/systemd/system/mycontainer.service
[Unit]
Description=My Docker Container
Requires=docker.service
After=docker.service
[Service]
Restart=always
ExecStart=/usr/bin/docker start -a my_container
ExecStop=/usr/bin/docker stop -t 2 my_container
[Install]
WantedBy=multi-user.target
Then enable it:
sudo systemctl enable mycontainer.service
When using Docker Compose, specify restart policies in your docker-compose.yml:
version: '3'
services:
webapp:
image: nginx:latest
restart: unless-stopped
ports:
- "80:80"
While supervisord works, it adds unnecessary complexity for Docker containers because:
- Docker already has process supervision built-in
- It creates an additional layer of process management
- Can interfere with Docker's own restart policies
- Makes debugging more complicated
After setting up, test by rebooting and checking:
sudo reboot
# After system comes back up
docker ps -a
Or for systemd services:
systemctl status mycontainer.service
Docker provides built-in functionality for container auto-start through its restart policies. This is the most straightforward method and should be your first consideration.
docker run -d --restart unless-stopped your_image
Available restart policies include:
no
: Don't restart (default)on-failure
: Restart only if container exits with non-zero statusalways
: Always restartunless-stopped
: Similar to always, but respects manual stops
For Ubuntu 14.04 (which uses upstart), we can create a systemd service file that manages our container:
[Unit]
Description=My Container
Requires=docker.service
After=docker.service
[Service]
Restart=always
ExecStart=/usr/bin/docker start -a my_container
ExecStop=/usr/bin/docker stop -t 2 my_container
[Install]
WantedBy=multi-user.target
Then enable it with:
sudo systemctl enable my-container.service
If you're using Docker Compose, you can specify restart policies in your docker-compose.yml
:
version: '3'
services:
webapp:
image: your_image
restart: unless-stopped
ports:
- "80:80"
Since Ubuntu 14.04 uses upstart by default, you can create an upstart job:
description "Start my_container"
start on filesystem and started docker
stop on runlevel [!2345]
respawn
script
/usr/bin/docker start -a my_container
end script
Save this as /etc/init/my_container.conf
and start it with:
sudo start my_container
After implementing any of these methods, test your setup:
sudo reboot
# After system comes back up
docker ps
This should show your container running if everything was configured correctly.
- For production systems,
unless-stopped
is generally preferred overalways
- Be mindful of container dependencies (database containers should start before app containers)
- Consider using health checks in your containers for more robust auto-restart behavior
- For complex setups, consider using orchestration tools like Kubernetes or Docker Swarm