Do `docker container rm` and `docker container kill` effectively achieve the same?

If you run a container..

eg

docker run alpine echo hello

It looks like it cleans up afterwards...

% docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

But it doesn't it's still there.

% docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
3a4772c0f165        alpine              "echo hello"        22 seconds ago      Exited (0) 20 seconds ago                       relaxed_ramanujan

This can be cleaned up with the rm command

% docker container rm 3a4772c0f165 
3a4772c0f165

% docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

So:

  • docker kill will kill a container.
  • docker rm will clean up a terminated container.

They are different things.

Note: you can tell containers to auto-clean:

% docker run --rm alpine echo hello
hello

% docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

Then you don't need to manually rm.


A container is (at least):

  • running processes
  • a top ephemeral layer, to its file-system.
  • volume and network mappings.

Kill will only deal with the first one.

If you run with --rm option. Then stopping, or killing the container, will also remove it.

You should not use kill (unless you have to), docker stop sends SIGTERM. (If you have to send SIGKILL to a process, this it is badly behaved, and need fixing.)

Tags:

Docker