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

Docker

Docker Problem Overview


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?

Docker Solutions


Solution 1 - Docker

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.

Solution 2 - Docker

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmkurnikovView Question on Stackoverflow
Solution 1 - DockerVonCView Answer on Stackoverflow
Solution 2 - DockermaheiView Answer on Stackoverflow