How to check if the restart policy works of Docker

一世执手 提交于 2021-01-21 09:14:27

问题


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


回答1:


you can also docker exec -it container_id bash and then kill -9 of the main process. I tested with docker run -d --restart=always -e DISPLAY=$DISPLAY -v /home/gg/moncontainer:/home/gg -v /tmp/.X11-unix:/tmp/.X11-unix k3ck3c/captvty I killed the main process (pid 5, Captvty.exe), was logged out of the container, and 2 seconds later it was restarted, the window was created again




回答2:


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>



回答3:


I just created a container manually, like this:

docker run -d --restart=always tacodata/pythondev sleep 10

note, that the daemon starts, but the container exits in 10 seconds. Everytime I do a docker ps I see:

core@pa2 ~ $ docker ps
CONTAINER ID        IMAGE                                     COMMAND             CREATED              STATUS              PORTS                    NAMES
69cbae4b6459        tacodata/pythondev:latest                 "sleep 10"          About a minute ago   Up 9 seconds        5000/tcp                 high_colden                                                                         

So, the container was created a minute ago, but the status shows it up only 9 seconds. It keeps restarting. You can get that information from:

core@pa2 ~ $ docker inspect high_colden
[{
"AppArmorProfile": "",
...
"Path": "sleep",
"ProcessLabel": "",
"ResolvConfPath": "/var/lib/docker/containers/69cbae4b645926b14d86effcfaaa7735119e7f0c8afb0baff5cc1913583bf35a/resolv.conf",
"RestartCount": 16,
"State": {
    "Error": "",
    "ExitCode": 0,
    "FinishedAt": "2015-04-16T16:36:15.325629703Z",
    "OOMKilled": false,
    "Paused": false,
    "Pid": 13453,
    "Restarting": false,
    "Running": true,
    "StartedAt": "2015-04-16T16:36:15.860163812Z"
},
"Volumes": {},
"VolumesRW": {}
}



回答4:


You can also restart docker service to see if it's firing up containers upon start. Under Ubuntu, for example,

sudo service docker restart


来源:https://stackoverflow.com/questions/29680274/how-to-check-if-the-restart-policy-works-of-docker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!