Restart policy detailsA restart policy only takes effect after a container starts successfully. In this case, starting successfully means that the container is up for at least 10 seconds and Docker has started monitoring it. This prevents a container which does not start at all from going into a restart loop.
Dockerfile Command to Keep the Container Running Method 1: You can use the -t (pseudo-tty) docker parameter to keep the container running. Method 2: You can run the container directly passing the tail command via arguments as shown below. Method 3: Another method is to execute a sleep command to infinity.
The reason our container is running after a reboot is because of the always policy. Whenever the Docker service is restarted, containers using the always policy will be restarted regardless of whether they were running or now.
Like the restart Docker command, Docker Compose includes the restart property to restart containers automatically.
You can use the --restart=unless-stopped
option, as @Shibashis mentioned, or update the restart policy (this requires docker 1.11 or newer);
See the documentation for docker update
and Docker restart policies.
docker update --restart=no my-container
that updates the restart-policy for an existing container (my-container
)
Use the below to disable ALL auto-restarting (daemon) containers.
docker update --restart=no $(docker ps -a -q)
Use the following to disable restart a SINGLE container.
docker update --restart=no the-container-you-want-to-disable-restart
Rational:
Docker provides restart policies to control whether your containers start automatically when they exit, or when Docker restarts. This is often very useful when Docker is running a key service.
Notes
If you are using docker-compose this might be useful to know.
restart no is the default restart policy, and it does not restart a container under any circumstance. When always is specified, the container always restarts. The on-failure policy restarts a container if the exit code indicates an on-failure error.
restart: "no"
restart: always
restart: on-failure
restart: unless-stopped
restart: always
You can start your container with --restart=unless-stopped
.
If you have a swarm restarting the containers, the swarm will restart any containers you stop or rm, irrespective of the restart option. That's a feature, not a bug.
Make sure you are not running a service you forgot about:
docker service ls
Then, you can stop the service
docker service rm <service id discovered with previous command>
Not a response to this question but to How to prevent docker from starting a container automatically on system startup?, which has been marked as a duplicate of this question.
If your container is started with restart=on-failure
and has a faulty command that exits with a non-zero exit code when you stop the container with docker stop
, it shows some weird behaviour: After stopping the container with docker stop
, the container is stopped, but after restarting the docker daemon (or the system), it is started automatically again. To fix this, either fix the command of the container or use no
or unless-stopped
as the restart policy.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With