Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if the restart policy works of Docker

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

like image 494
Browny Lin Avatar asked Apr 16 '15 16:04

Browny Lin


People also ask

How to set a restart policy for a docker container?

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:

What is the difference between'always restart'and'unless stopped'in Docker?

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.

How do I manage the lifecycle of a docker container?

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.

How to see restart policy of a running container(s)?

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.


1 Answers

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>
like image 191
Aliance Avatar answered Oct 20 '22 14:10

Aliance