2

Docker sometimes exists but still keeps running.

Started with :

docker run -v $PWD:/host --rm -it Ubuntu_2018

After "exit" : Docker container ls; docker container ls -al

This still shows container running How to make sure that docker container is gracefully ended?

Using the option "--rm" and exiting the docker with "exit" should do the needful. But it still sometimes remains.

After exit, "docker container ls; docker container ls -al" should not show the docker at all

Lakmi
  • 1,379
  • 3
  • 16
  • 27

1 Answers1

1

How to ensure confirmed graceful exit of docker?

Not by looking at the docker container ls -al output, which as commented shows the stopped containers.

But by looking at:

  • the logs of a stopped container, for an application message stating the stop was graceful (so that depends more on the containerized application, less on Docker itself).
  • its Exit status code: docker inspect -f '{{.State.ExitCode}}' <container SHA>

For that, see "Gracefully Stopping Docker Containers" by Brian DeHamer, who reminds us that When you use docker stop or docker kill to signal a container, that signal is sent only to the container process running as PID 1.

See also "Life and death of a container" from Luis Herrera Benítez who points out the existence of "docker inspect --format='{{.State.Health.Status}}' <containerName>": if it is unhealthy... chances are your subsequent stop might not be graceful.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250