Docker containers make for a lightning-fast means of rolling out an app or service to your data center. In seconds, you can deploy a container for just about anything. And, if created properly, that container will run without fail.

Until your server goes down. Maybe you’ve upgraded the kernel and need to reboot. Or maybe you had a power failure or are migrating hardware. Whatever the reason, once that server comes back up, unless you’ve deployed the container properly, you’ll have to re-deploy.

That’s the big question: how do you deploy a Docker container so that it’ll automatically restart after your server is up and running again? It’s much easier than you might think.

SEE: Kubernetes: A cheat sheet (free PDF) (TechRepublic)

To handle this, Docker has what is called Restart Policies, of which there are four. These policies are passed through the docker command and dictate how the deployed container will handle a restart. The four policies are:

  1. no: Do not automatically restart the container.
  2. on-failure: Restart the container, only if it exits due to an error.
  3. always: Always restart the container if it stops, or is manually stopped due to the daemon stopping.
  4. unless-stopped: Always restart the container, unless the daemon is stopped, at which point, the container must be restarted manually.

It is important to understand the following about restart policies:

  • Restart Policies only take effect if a container successfully starts. Once a container is successfully running, after 10 seconds the Docker will start monitoring it and will apply the associated Restart Policy. If a container fails to deploy, the Restart Policy does not come into play.
  • Also, if you manually stop a container, the Restart Policy is ignored until the daemon itself is restarted.
  • Restart Policies only apply to containers and no other service or application.

How to apply a Restart Policy

Let’s say you want to deploy an NGINX container and you want to ensure it always restarts. For this, you’d want to apply the always policy. Without the Restart Policy, our NGINX container deployment would look like this:

docker run --name docker-nginx -p 8080:80 -d nginx

The above command would deploy the NGINX container, named docker-nginx, in detached mode with external port 8080 pointing to internal port 80. Should the server go down, or the Docker daemon stop, that container would go down and not automatically restart. However, if we deploy that container like so, it will always restart:

docker run --name docker-nginx -p 8080:80 -d nginx --restart always

You can apply whatever Restart Policy you prefer after the –restart option, so:

  • –restart on-failure
  • –restart always
  • –restart unless-stopped

Note: You don’t have to set the –-restart no policy, as that is the default.

If you use Docker Compose, you would add a Restart Policy into your YAML file like so:

services:
nginx:
image: nginx:latest
restart: always

If you change your mind about a Restart Policy, you can always use the update command. Say you want to change from always to unless-stopped. The command for that would be:

docker update --restart unless-stopped docker-nginx

Of course, you’d change docker-nginx to the name of the container you’re looking to change.

Which policy to choose?

Why would you choose one over another? The most obvious is the unless-stopped option. If you tend to manually stop containers, this is the Restart Policy you should choose. For example, you might use particular containers during particular times of the day.

Say you have ContainerX for morning, ContainerY for afternoon, and ContainerZ for evening. If you set the policy to always and you stop ContainerX, should the Docker daemon restart for any reason, ContainX will start back up, even when ContainerY is running. If you use the unless-stopped option, those containers you manually stop will remain stopped until you manually restart them.

Other than that, the choice between policies can be a bit of a gray area. Either way, you now can keep those containers running at all times.

Subscribe to TechRepublic’s How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.


Image: Docker

Subscribe to the Data Insider Newsletter

Learn the latest news and best practices about data science, big data analytics, artificial intelligence, data security, and more. Delivered Mondays and Thursdays

Subscribe to the Data Insider Newsletter

Learn the latest news and best practices about data science, big data analytics, artificial intelligence, data security, and more. Delivered Mondays and Thursdays