Reputation: 515
I'm new to docker. Would like to know about the ideal ways to cleanup docker containers.
To clean up containers I'm simply stopping container and just removing it using docker rm
command.
But it seems like docker is eating hard disk space. I been through Why is docker image eating up my disk space that is not used by docker article. But is there any other way to do the same. Please suggest.
Upvotes: 3
Views: 307
Reputation: 878
Best way to Cleanup docker container and especially in a PROD environment
1: docker ps -a
2: Clean up things that you are not used by any resource with docker system prune -a
3: Last remove anything that needs to be removed with
PS : Be careful as anything that will be removed there is no way back!
docker rm $(docker ps -a -q)
docker rmi $(docker images -q)
Upvotes: 0
Reputation: 9961
If all you want to do is run a container once, and delete it when done, invoke it with the --rm
option. The container will be automatically removed after exit.
docker run --rm ubuntu:14.04 cat /etc/lsb-release
If you monitor docker ps -a
you will see the container being launched and then removed.
Upvotes: 0
Reputation: 4963
Stop all the containers (docker stop <containerId>
) you don't need anymore and run those two commands:
docker rm $(docker ps -a -q)
docker rmi $(docker images -q)
The first command will delete all inactive containers (the others will pop up errors, but they can be ignored).
The second command removes all images that are not used by any container (also the non actives).
Note: The next time you start a container you have to download all the layers again.
As Mark O'Connor pointed out there is also the -v
option to remove associated volumes. Make sure you don't loose any important data with that!
docker rm -v $(docker ps -a -q)
From the Docker rm docs:
-f, --force=false Force the removal of a running container (uses SIGKILL)
-l, --link=false Remove the specified link
-v, --volumes=false Remove the volumes associated with the container
Upvotes: 1
Reputation: 46480
It's worth looking at the docker-gc project that Spotify came up with to address this problem.
docker-gc essentially just automates running of the commands suggested in the other answers, but also has support for whitelisting images etc.
Upvotes: 4
Reputation: 49181
List running images
docker ps
Stop them
docker stop [container name or id]
List stopped
docker ps -a
Remove stopped
docker rm [container name or id]
List your images
docker images
Remove images
docker rmi [image name or id]
Upvotes: 0