How do I autostart docker container at system reboot?

As I hosted a WordPress site in docker containers and i want these containers to be started at boot time always, so need to done it manually.


It is a very common use case to add the restart policy on an existing container.

This could be done with the following command: docker update --restart {no,on-failure,unless-stopped,always} container_name

More details: Docker - Start containers automatically


Till now I don't think there is a way to do that normally. A tricky solution is to use restart policy

sudo docker run --restart=always -d your_image

This means whenever you shut down this will exit your container so as you start your host then this lead to restart the docker.


As ubuntu 15 now supports systemd, sample for this manager:

someservicename.service

[Unit]
Description=Some service
Requires=docker.service
After=docker.service

[Service]
Restart=always
ExecStart=/usr/bin/docker start -a container_name
ExecStop=/usr/bin/docker stop -t 2 container_name

[Install]
WantedBy=multi-user.target

More available on docker site

P.S. Pretty cute config :)


What I did is to use Upstart files.

You can find some examples and other solutions in the Docker website.

Create a file such like that in /etc/init :

Description "My container"
start on filesystem and started docker
stop on runlevel [!2345]
respawn
script
   /usr/bin/docker start -a mycontainer
end script
pre-stop script
  /usr/bin/docker stop mycontainer
end script

Note, as from Docker 1.2, there are restart policies which may also help to automatically restart containers when the docker service is run (after boot for example).

Personnaly, I use puppet to provision my workstation and use this Docker module to automatically create the startup scripts which are more complete (start, stop, restart, clean options...)