How do you set a container to automatically start in Docker?

The container’s auto-start can be set through the “–restart” flag in the “docker run” command. The “–restart” flag has several options to choose from.

  1. No: Container will not automatically restart if it exits.
  2. Always automatically restarts if the container exits.
  3. – If the container exits with a non-zero status, it will automatically restart.
  4. Automatically restarts unless manually stopped.

For example, to have a container automatically start and always restart when the Docker daemon starts, you can use the following command:

docker run --restart=always [image_name]

To change the existing container’s auto-restart setting, you can use the docker update command. For example, to set a container to always restart automatically, you can use the following command:

docker update --restart=always [container_id]

Please note that in order to use these features, you need to have Docker version 1.12 or higher.

bannerAds