Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between "docker stop" and "docker rm"?

Tags:

docker

I initially thought that docker stop is equivalent to vagrant halt, and docker rm is to vagrant destroy.

But fundamentally, docker containers are stateless, except for VOLUME statement, which AFAIK preserves directory content even after docker rm, if it wasn't called with -v.

So, what is the difference?

like image 823
mkurnikov Avatar asked Oct 27 '15 08:10

mkurnikov


People also ask

What is the difference between docker stop and docker kill?

To terminate a container, Docker provides the docker stop and docker kill commands. Both the docker kill and docker stop commands look similar, but their internal execution is different. The docker stop commands issue the SIGTERM signal, whereas the docker kill commands sends the SIGKILL signal.

What does docker rm mean?

On Docker, --rm option means automatically remove the container when it exits.

Does docker rm stop container?

Removing Docker ContainersDocker containers are not automatically removed when you stop them unless you start the container using the --rm flag.

When should I use docker rm?

The flag --rm is used when you need the container to be deleted after the task for it is complete.


2 Answers

docker stop preserves the container in the docker ps -a list (which gives the opportunity to commit it if you want to save its state in a new image).

It sends SIGTERM first, then, after a grace period, SIGKILL.

docker rm will remove the container from docker ps -a list, losing its "state" (the layered filesystems written on top of the image filesystem). It cannot remove a running container (unless called with -f, in which case it sends SIGKILL directly).

In term of lifecycle, you are supposed to stop the container first, then remove it. It gives a chance to the container PID 1 to collect zombie processes.

like image 185
VonC Avatar answered Oct 22 '22 13:10

VonC


docker rm removes the container image from your storage location (e.g. debian: /var/lib/docker/containers/) whereas docker stop simply halts the the container.

like image 20
mahei Avatar answered Oct 22 '22 12:10

mahei