From the Docker document, there is a restart policy
parameter could be set.
How do I verify the container indeed restarts when the container exits. How to trigger the exit of container manually, and observe if the container restarts?
My environment is Mac and boot2docker.
Thanks
To use restart policies, Docker provides the following options: on-failure [:max-retries]: Restart the container if it exits with a non-zero exit code and provide a maximum number of attempts for the Docker daemon to restart the container Now, let's look at an example of how to set a restart policy using the Docker CLI for a single container:
But the main difference between the two is that if you stop the containers with docker stop command and then restart the docker daemon, the container with always restart policy will start the container automatically but the container with unless-stopped policy won't be restarted. Let me show it with examples.
Docker gives you several options to manage your container’s lifecycle. Containers do not normally restart automatically after they terminate. With restart policies, you can take control over individual container lifecycles. Restart policies will be used whenever a container stops running.
Does anyone know how to see the restart policy of a running container (s)? Yes, it is possible using docker inspect which is json format and just need to query it. Here is relevant output of docker inspect for a running container zen_easley. Note to change container name as suitable for your environment.
After running the container you can inspect its policy, restart coun and last started time:
docker inspect -f "{{ .HostConfig.RestartPolicy }}" <container_id>
docker inspect -f "{{ .RestartCount }}" <container_id>
docker inspect -f "{{ .State.StartedAt }}" <container_id>
Then you can look into container processes:
docker exec -it <container_id> ps -aux
The PID 1 process - is the main process, after its death the whole container would die.
Kill him using
docker exec -it <container_id> kill -9 <pid>
And after this ensure that the container autorestarted:
docker inspect -f "{{ .RestartCount }}" <container_id>
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