How to ensure confirmed graceful exit of docker? - docker

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

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.

Related

What to do if the docker container hangs and does not respond to any command other than ctrl+c?

I have been running a nvidia docker image since 13 days and it used to restart without any problems using docker start -i <containerid> command. But, today while I was downloading pytorch inside the container, download got stuck at 5% and gave no response for a while.
I couldn't exit the container either by ctrl+d or ctrl+c. So, I exited the terminal and in new terminal I ran this docker start -i <containerid> again. But ever since this particular container is not responding to any command. Be it start/restart/exec/commit ...nothing! any command with this container ID or name is just non-responsive and had to exit out of it only after ctrl+c
I cannot restart the docker service since it will kill all running docker containers.
Cannot even stop the container using this docker container stop <containerid>
Please help.
You can make use of docker RestartPolicy:
docker update --restart=always <container>
while mindful of caveats on the docker version you running.
or explore an answer by #Yale Huang from a similar question: How to add a restart policy to a container that was already created
I had to restart docker process to revive my container. There was nothing else I could do to solve it. used sudo service docker restart and then revived my container using docker run. I will try to build the dockerfile out of it in order to avoid future mishaps.

How to investigate why container exit in restart=always mode?

I have container restarted itself 3 times and still working.
How can I know why it restarted? and how i can see the logs for each time the container has exit?
You can look for the id with:
docker ps -a
Let's say you see your container with Exited status, and you can check the logs of that container with
docker logs your_container_here
You can also follow the logs by
docker logs -f your_container_here
You can check logs of stopped container by container id with docker logs command
sample :
docker logs 99e691c271c9
and You can get container ids by running
docker ps -a (for all containers)
or
docker ps -al (for latest/last stopped container)

docker ps shows empty list - although docker telling container exists

when calling docker ps the list is empty, although I got an id:
(dcbb6aeaa06ba43fcb.....)
My steps:
Step 1: I created an image (imagekommando) of an running js.file:
Step 2: I created a container (in background) based on my image
docker run -d --name containerkommando imagekommando
I got an id! (container-id??)
Step 3: But docker ps shows empty list:
But when I repeat Step 2, I'm told, that the container (containerkommando) already exists:
docker run -d --name containerkommando imagekommando
Could you help me, understanding the logic behind?
And how can I get the container running (by ID)?
That means that the docker container exited with an error but clean up is required. With --rm option you can tell the docker to remove the container when the container has exited.
docker run --rm .....
Also to check the reason for the container exiting...you can use
docker logs <container_id>
What probably takes place here:
docker run ... creates and starts your container
your container exits
docker ps doesn't list stopped containers (default shows just running), so it made you think that it's not there.
docker run ... fails because you are trying to create and run a container with a name that already exists.
Further reading:
What are the possible states for a docker container?
Why docker container exits immediately
In Docker, a container is automatically exited when the task is finished. You have to specify a correct entrypoint to keep your docker container up.
You can check the exited containers with the command docker ps -a. This exited container will prevent you from using the name again.
So, you may want to use docker rm <container-name> before creating your new container. In a test environement, you can also use docker system prune to clean all unused container/networks.
docker ps only shows the active containers (the running ones).
Your container most probably exited right after you started it. You can use the container ID and do docker logs <container-id> to examine the reason why the container failed.
If you want to see the stopped containers together with the running containers you can do docker ps -a to get a list of all these.
Execute
docker logs <CONTAINER ID>
to view the logs of docker container run.
I faced a similar issue found out there was space issue win my docker. After clearing space the container was able to run.

Docker ps shows running container that I cannot stop or kill. How to stop/kill that container?

"Docker p"s shows 1 running container. The command "docker ps --filter status=running" shows the same process.
When trying to stop the container, I get the identification but nothing happens. The container is still running.
When I performing a 'docker kill id' I get:
Error response from daemon: Cannot kill container: xyz: Container
2f-etc is not running
I cannot perform a 'docker container rm name' because of this message:
Error response from daemon: driver "overlay2" failed to remove root
filesystem for
2f-etc:
remove
/var/lib/docker/overlay2/0877e30fd3a98f46b981827b52aec02a7004649e2a9c01c72e38cc8de5b309b0/diff/tmp/hsperfdata_root/5:
read-only file system
How to stop / kill that container?
What do I know more:
It may be that I stopped my computer without stopping the container. I will take care ;-)
I noticed also that creating a new image with 'docker build -t newname .' is failing because: Error response from daemon: mkdir /var/lib/docker/tmp/docker-builder471922504: read-only file system
Restart docker is simple but global solution. In my case I needed to keep other containers working without stop and the solution of #juanlumn helped me, which is docker container rm -f name (it removes container).
As #Aderemi Dayo suggested - a restart of the Docker machine suffices.
Sometimes docker restart doesn't work, so you may have to individually run docker stop & docker start. Thereafter use the docker container rm <container> command to remove.

How do I kill the docker processes?

I have tried
docker kill name_of_the_process
But the error is
Error response from daemon: Cannot kill container: name_of_the_container: Container name_of_the_container is not running
There are a lot of processes when I run docker ps -a, how do I kill those, they are stopped but not killed
A stopped container is killed. There is no running process, but there is a writable container specific filesystem and some metadata remaining which allows you to debug the stopped container and restart it. To remove that, use docker container rm (or the former alias docker rm) to remove the stopped container data. e.g.
docker container ls -aqf status=exited | xargs docker container rm
docker ps Shows you the running containers. If you add the -a flag it will print out all the containers, even the ones not running.
You can not "stop" a non-running container, it is already stopped.
You can find more information here: https://docs.docker.com/engine/reference/commandline/ps/
If you wish to remove the container, you can remove it by id or name. The command for that is docker rm <id/name>
Tip: You can use the first few chars of the id to identify the container, you don't need the whole id

Resources