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

Browny Lin
  • 2,427
  • 3
  • 28
  • 32

5 Answers5

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>
alper
  • 2,919
  • 9
  • 53
  • 102
Aliance
  • 847
  • 1
  • 11
  • 23
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

user2915097
  • 30,758
  • 6
  • 57
  • 59
  • cool, I learn a new command `docker exec -it 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 :) – Browny Lin Apr 16 '15 at 22:49
  • I found the process should be killed is about the process ran in ENTRYPOINT or CMD – Browny Lin Apr 16 '15 at 23:04
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": {}
}
Greg
  • 6,571
  • 2
  • 27
  • 39
  • 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? – Browny Lin Apr 16 '15 at 22:55
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
Baris Demiray
  • 1,539
  • 24
  • 35
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>.

Sebastian Lang
  • 492
  • 1
  • 6
  • 18