How to check if the restart policy works of Docker
Asked Answered
C

5

9

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

Carbajal answered 16/4, 2015 at 16:7 Comment(0)
A
7

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

Alanealanine answered 16/4, 2015 at 19:46 Comment(2)
cool, I learn a new command docker exec -it <container_id> bash to get into the container shell. Thanks, this method works, a little inconvenience is I don't know what process I should kill to cause exit, so I try one by one. Anyway, it restarts indeed :)Carbajal
I found the process should be killed is about the process ran in ENTRYPOINT or CMDCarbajal
P
25

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 it using:

docker exec -it <container_id> kill -9 <pid>

And after this ensure that the container autorestarted:

docker inspect -f "{{ .RestartCount }}" <container_id>
Peptide answered 29/1, 2016 at 14:8 Comment(1)
You can also do docker kill <container_id> which will send a SIGKILL to the container by default.Erl
A
7

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

Alanealanine answered 16/4, 2015 at 19:46 Comment(2)
cool, I learn a new command docker exec -it <container_id> bash to get into the container shell. Thanks, this method works, a little inconvenience is I don't know what process I should kill to cause exit, so I try one by one. Anyway, it restarts indeed :)Carbajal
I found the process should be killed is about the process ran in ENTRYPOINT or CMDCarbajal
H
1

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": {}
}
Headsail answered 16/4, 2015 at 16:37 Comment(1)
Thanks, this is also a good solution. But, sleep should only trigger the zero exit status exit right? Is there other way to trigger non-zero exit status exit?Carbajal
G
0

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

sudo service docker restart
Galah answered 16/6, 2018 at 11:31 Comment(0)
M
0

Another alternative:

  • Get the Container ID of the container to be killed with docker ps.

  • Then do ps -efa | grep <Container ID> to get the Process ID.

  • Then do sudo kill -9 <Process ID>.

Maure answered 10/9, 2021 at 20:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.