155

Every docker run command, or every RUN command inside a Dockerfile, creates a container. If the container is no longer running it can still be seen with docker ps -a.

Should I be concerned with having an enormous list of non-running containers? Should I be issuing docker rm on non-running containers?

I am unsure of what performance or memory/storage penalties these non-running containers incur.

Ben
  • 51,770
  • 36
  • 127
  • 149
velo9
  • 2,295
  • 2
  • 20
  • 21
  • 1
    A `docker exec` command has been added a while ago, see https://docs.docker.com/reference/commandline/cli/#exec - it will execute a command off a running container. – schmunk Dec 26 '14 at 23:18
  • FYI, if you want to see how to remove old containers, see this question: http://stackoverflow.com/questions/17236796/how-to-remove-old-docker-containers/29474367 – Ryan Walls Sep 08 '15 at 16:24
  • 1
    @schmunk The link has moved. https://docs.docker.com/engine/reference/commandline/exec/ – akauppi Jan 20 '17 at 05:40

5 Answers5

80

The containers that are not running are not taking any system resources besides disk space.

It is usually good to clean up after yourself, but if you have a lot of them sitting around it shouldn't slow down performance at all.

If you do notice a slow down when running docker commands with lots of stopped containers, it might be a bug in docker, and you should submit a bug.

Ken Cochrane
  • 75,357
  • 9
  • 52
  • 60
  • 1
    The docs for the `RUN` command have now moved to: http://docs.docker.io/en/latest/reference/builder/#run – aculich Feb 20 '14 at 03:24
71

The docker run documentation describes how to automatically clean up the container and remove the file system when the container exits:

  --rm=false: Automatically remove the container when it exits (incompatible with -d)

The above shows that by default containers are not removed, but adding --rm=true or just the short-hand --rm will work like so:

sudo docker run -i -t --rm ubuntu /bin/bash

When you exit from the container it will be automatically removed.

You can test this by listing your docker containers in one terminal window:

watch -n1 'sudo ls -c /var/lib/docker/containers'

And then in another window run this command to run multiple docker containers that will all automatically exit after sleeping for up to 10 seconds.

for i in {1..10}; do sudo docker run --rm ubuntu /bin/sleep $i & done
laura_b
  • 119
  • 8
aculich
  • 14,545
  • 9
  • 64
  • 71
  • 5
    Not a direct answer to the *question*, but an interesting answer that will give users some insight into the mechanisms behind docker. Thanks! – thaJeztah Jun 05 '14 at 09:29
  • 3
    `-rm` is deprecated and will be removed at some time, use `--rm` instead. – bain Jul 20 '15 at 19:02
5

If you run a container with a volume and do not use docker rm -v to remove it then the volume is not being removed after you remove a container. Also there is an issue with a vfs storage driver. If you forget to clean, volumes will eat up your disk space.

zergood
  • 711
  • 1
  • 10
  • 15
4

I am unsure of what performance or memory/storage penalties these non-running containers incur.

In order to assess how much storage non-running Docker containers are using, you may run:

docker ps --size --filter "status=exited"

Equivalently, you could run: docker container ls --filter "status=exited"

You may also use the command docker system df (introduced in Docker 1.13.0, January 2017) to see docker disk usage, e.g.:

username@server:~$ docker system df
TYPE                TOTAL               ACTIVE              SIZE                RECLAIMABLE
Images              44                  28                  114.7GB             84.84GB (73%)
Containers          86                  7                   62.43GB             41.67GB (66%)
Local Volumes       2                   1                   0B                  0B
Build Cache                                                 0B                  0B
user9269906
  • 111
  • 1
  • 6
Franck Dernoncourt
  • 77,520
  • 72
  • 342
  • 501
2

An image is a snapshot of file system and dependencies or a specific set of directories of a particular application/software. By snapshot I mean, a copy of just those files which are required to run that piece of software (for example mysql, redis etc.) with basic configurations in a container environment.

When you create a container using an image, a small section of resources from your system are isolated with the help of namespacing and cgroups, and then the files inside the image are copied in this isolated environment of resources.

Hence, containers only take disk space. If there are unused containers, you should remove them, but not the image as they always reusable.

aryanveer
  • 628
  • 1
  • 6
  • 17