9

From manpages:

docker container rm will remove one or more containers from the host node. The container name or ID can be used. This does not remove images.

docker container kill: The main process inside each container specified will be sent SIGKILL, or any signal specified with option --signal.

Is a container a running instance of an image? So do docker container rm and docker container kill effectively achieve the same: the container will stop existing?

What are their differences?

What is "the main process inside a container"?

Is a container run exactly as a process in the host machine?

Thanks.

Tim
  • 101,790

2 Answers2

12

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.

  • Can you elaborate, what does it mean that docker "cleans"? What do really happens when a container is "uncleaned" and shows up with the "Exited 0" status? – Richard Miller Sep 29 '20 at 16:50
7

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.)