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.
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.
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.
In order to assess how much storage non-running Docker containers are using, you may run:
--size
: display total file sizes (FYI: Explain the SIZE column in "docker ps -s" and what "virtual" keyword means #1520).--filter "status=exited"
: list only stopped containers.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.:The
docker run
documentation describes how to automatically clean up the container and remove the file system when the container exits:The above shows that by default containers are not removed, but adding
--rm=true
or just the short-hand--rm
will work like so:When you exit from the container it will be automatically removed.
You can test this by listing your docker containers in one terminal window:
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.