121

What is a good way to automatically start docker containers when the system boots up?

Is there a preferred way to do this on Ubuntu 14.04?

I've used supervisord in the past to auto start web apps. But that doesn't feel like the right thing for Docker.

4 Answers 4

149

Apparently, the current method to auto-start Docker containers (from Docker 1.2) is to use restart policies. This will control how Docker should handle starting of the container upon startup and re-starting of the container when it exits. I've used the 'always' option so far, and can confirm that it makes Docker auto-start the container at system boot:

sudo docker run --restart=always -d myimage

Documentation Excerpt

Restart Policies Using the --restart flag on Docker run you can specify a restart policy for how a container should or should not be restarted on exit.

no - Do not restart the container when it exits.

on-failure - Restart the container only if it exits with a non zero exit status.

always - Always restart the container regardless of the exit status.

You can also specify the maximum amount of times Docker will try to restart the container when using the on-failure policy. The default is that Docker will try forever to restart the container.

$ sudo docker run --restart=always redis

This will run the redis container with a restart policy of always so that if the container exits, Docker will restart it.

$ sudo docker run --restart=on-failure:10 redis

This will run the redis container with a restart policy of on-failure and a maximum restart count of 10. If the redis container exits with a non-zero exit status more than 10 times in a row Docker will abort trying to restart the container. Providing a maximum restart limit is only valid for the on-failure policy.

5
  • 14
    "always - Always restart the container regardless of the exit status" is a bit confusing. It won't restart the container if you manually exit/stop the container, which is the behavior I was looking for.
    – w00t
    Mar 27, 2015 at 23:55
  • 14
    Note: another policy called unless-stopped was added. It acts like always but If the container is stopped and the system is rebooted or the docker daemon is restart, the container will not restart. See here for a nice write up of all 4 options blog.codeship.com/… Jun 23, 2016 at 22:56
  • 5
    Of course, the docker daemon must auto-start to support this.
    – sherrellbc
    Jan 23, 2017 at 17:03
  • 1
    I think the question is asking "on system boot", meaning after the physical or virtual server rebooted, how the containers auto-restart, assuming docker engine is fully running after the server reboot?
    – Root Loop
    Aug 2, 2019 at 2:22
  • Exactly @RootLoop. People read poorly Jan 29, 2020 at 15:05
10

Docker has this page that explains how to do it with upstart and systemd. I agree that it doesn't seem like the right thing for Docker. Their solution is to run docker start, which assumes that you've already created your container. I would think that you'd either do docker run --rm in the upstart script (treating it like a brand new process and container from an image) or just let the docker daemon restart the containers itself on boot (as it will by default if you do nothing else). Upstart has the advantage of allowing easy start/stop of processes, but you get that with docker's start/stop too!

I think it's weird to force the user to manually create a container (with all the correct port/volume bindings) before the upstart script will work.

2
  • The link is broken... This seems like a possible replacement, but it certainly does not show "how" Apr 16, 2019 at 7:41
  • Thanks, I've fixed the link to a similar page, but I can't be sure that it says the same thing that the original did. Apr 16, 2019 at 14:18
6

But that doesn't feel like the right thing for Docker.

Why not?

I use supervisord for this with great success.

Use what you know, use what works, use something that you can easily maintain and understand.

4
  • Thanks @EEAA .. does that mean you run them in non-daemon mode? Doesn't that also mean that you need to run them with --rm ? Oct 2, 2014 at 14:30
  • I run the containers in foreground mode and let supervisord catch stdout/stderr. I'm not sure why --rm is relevant here.
    – EEAA
    Oct 2, 2014 at 14:32
  • @EEAA: about your question: For some people, docker is a replacement for lxc or openvz which have lxc.start.auto = 1 and vzctl set --onboot yes. Also ESXi and other virtualization solutions have such a feature included. Like Lawrence, I also don't think such an autostart feature should be implemented in a distribution-specific way because a docker user should be able to solve the same problem with the same knowledge on every platform. Dec 15, 2016 at 10:18
  • 1
    Right, Docker is a great way to decouple the host from running containers so using host-specific configuration is a bit of a step backwards.
    – nijave
    Nov 18, 2017 at 18:59
0

Updated Solution for Ubuntu 22.04 - 2022

Solution TL;DR:

  1. Enable docker to start the daemon:

sudo systemctl enable docker.service

  1. Enable docker container to start on restart

docker update --restart=always [container id or container name]


I thought I'd add an updated version of the solution for those looking.

I'm running a Docker Container on Ubuntu 22.04. The container houses ubuntu 20.04 and SQL Server. I was looking to have my SQL start up automatically if the machine was rebooted.

I followed the instructions on this page and has worked well for my use case.

Credit for the above solution is to that link.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.