60

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?

mkurnikov
  • 1,581
  • 2
  • 16
  • 19

2 Answers2

76

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.

codeforester
  • 39,467
  • 16
  • 112
  • 140
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 2
    Do I lose container state if I run `docker stop`, then `docker start`? I thought I do. – mkurnikov Oct 27 '15 at 08:37
  • @mkurnikov from http://stackoverflow.com/a/21928864/6309, you don't lose your changes. – VonC Oct 27 '15 at 08:47
  • Ok, so this commands _are_ equivalent to `vagrant` commands after all. – mkurnikov Oct 27 '15 at 08:57
  • Some may also find helpful: `docker rmi ` will remove the image (from which the container was created from) – timhc22 May 31 '17 at 14:36
  • after successfully doing both: `docker stop some-postgress` and `docker rm some-postgress`, where some-postgress is the name of my postgress db, i can still see the container in `docker ps -a`. i think this is the case because you might have referenced the images rather than given container names. – moldovean May 19 '20 at 10:58
  • @moldovean In which state that container is? Exited? – VonC May 19 '20 at 11:00
  • Is it same with podman stop ? – Shivam Papat May 27 '22 at 11:09
  • @ShivamPapat it should, even though there is a bug for containers started with `--rm` as an option: https://github.com/containers/podman/issues/11384 – VonC May 27 '22 at 15:16
5

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.

mahei
  • 85
  • 1
  • 3
  • 1
    I have tried running `docker images | grep container_name` before and then after running `docker rm -f container_name` and I got the same one_line output. So, it only stops the container; this does not remove the image used to run the container. Or am I getting something wrong? – M.Ionut Sep 14 '21 at 09:36