Tips&Tricks

How to automatically run Docker containers at startup

docker

Docker is a container technology for Linux / MacOS / FreeBSD (and even Windows, but not fully supports) that allows a developer to package up an application with all of the parts it needs.

Restart Policies

You should add --restart=always argument:

docker run --restart=always -d {{ name }}

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.

$ docker run --restart=always {{ name }}

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

$ docker run --restart=on-failure:10 {{ name }}

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.