docker clean up container overhead - docker

I have two docker containers running, where the diskspace is as follows:
user$ sudo docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 2 1 448.3MB 1.84kB (0%)
Containers 2 2 284.4MB 0B (0%)
Local Volumes 7 2 418.8kB 176.4kB (42%)
Build Cache 0 0 0B 0B
If I check diskusage I get:
root:/var/lib/docker# du -ah --max-depth=1
45G ./containers
What can I do to keep the containers (since I made a lot of configuration) but remove the obvious overhead?

Please run the below command the clean the system as
This will remove:
all stopped containers
all networks not used by at least one container
all dangling images
all dangling build cache
docker system prune -f
And yes, you can remove the son file as well.

You can use docker system prune -f, but, from my experience, it is not cleaning all that can be cleaned up.
For that, I've created a simple Docker image that does all the needed cleaning using only one command:
docker run -v /var/run/docker.sock:/var/run/docker.sock tikalci/tci-docker-cleanup:latest
For more details, see: https://github.com/TikalCI/tci-docker-cleanup

Related

How to find active docker images and volumes?

I can see docker disk using the docker system df command:
$ docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 31 1 12.86GB 12.81GB (99%)
Containers 1 0 0B 0B
Local Volumes 25 1 17.24GB 17.19GB (99%)
Build Cache 244 0 6.249GB 6.249GB
The output shows one image and one volume are active. How can I find these active objects?
I don't think I have any active objects because docker ps displays no results. Perhaps these are internally managed docker objects?
docker ps -a solved the problem

Running a Docker Container in Different hard Disk Partition and keeping the Docker Images on Different Hard Disk Partition

My Problem is i have Ubuntu Machine , and 2 partitions root and home . I have a Docker Image of MySQLDB which of 100GB. I keep them under root -> /var/lib/docker. Docker images itself uses 100GB of my root partition.
Now If I run this particular docker image -> Container gets created and tries to use another 100 GB of Hard Disk on root partition while running the Docker Container.so 200 GB it uses from root partition.
Is there anyway i can keep the docker images on root partition, and while running them, i want the container to use the other partition in hard disk (not the same where the images are stored).
I am not sure whether this is feasible.
Thanks in Advance for the help.
make a soft link for image folder
systemctl stop docker
mv /var/lib/docker/image /partition_2/
ln -s /partition_2/image /var/lib/docker/image
systemctl start docker

What is build cache in `docker system df`

run docker system df will display a row of Build Cache. What does this mean? In my machine this line is always showing 0 for all fields.
$ sudo docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 5 3 352.9MB 352.9MB (100%)
Containers 7 0 26.29MB 26.29MB (100%)
Local Volumes 1 1 0B 0B
Build Cache 0 0 0B 0B
The Build Cache lines refer to the cache used by BuildKit which is included with 18.09 and newer versions of docker. It is not enabled by default, so unless you have switched it on, you can expect this to read 0. This is the cache used when building and rebuilding images to speed up builds and reuse shared layers between images. It also reduces the size of the images pushed to a registry when layers are reused from prior builds.
The cache from BuildKit is buried since it runs from containerd rather than directly in docker, so you can view the disk used for this cache and then prune it with commands like:
docker builder prune
If you run builds without BuildKit, the cache for these will be cleaned up when you prune images on the host.
The command docker system df shows the docker disk usage.
Images shows the disk usage for the docker images that are not running.
Containers shows the disk usage for the docker containers running.
Local Volumes shows the disk usage for the volumes you are using on your running containers.
And, recently, it was added a new section called Build Cache, which shows the disk usage for the cache files docker is using while building and running containers.
It was not there before, it was added on May 18, 2018, but they forgot to add it to the documentation, so you can't see it listed on the system df docs.
I'd just sent a PR so you can see it on the example output so I hope they can merge it soon.
Edit:
The PR was merged, you can now find the examples on the official documentation.

What is the "RECLAIMABLE" space displayed in docker system df?

One can use the command docker system df (mirror) (introduced in Docker 1.13.0) 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
How is the "RECLAIMABLE" displayed in docker system df computed? I.e., what does it represent?
The Docker documentation on docker system df (mirror) doesn't explain it. The Docker glossary (mirror) doesn't contain the term "RECLAIMABLE".
Hi #Franck Dernoncourt!
RECLAIMABLE is the space consumed by "unused" images (in the meaning of no containers based on thoses images is running).
In other words and as #jordanm said, this is the total size of images you can remove without breaking anything, that is exactly why Docker will remove them if you run docker system prune -a or docker image prune -a. The -a tells Docker to remove all unused images, without it Docker only removes dangling (untagged) images.
You can learn more on how optimize your disk space with Docker here and here and of course Docker documentation for docker image prune and docker system prune.
It's worth mentioning in addition to Kerat's answer, the command you may be looking for to free up space listed as RECLAIMABLE is docker system prune -a --volumes. Volumes will not be pruned by default if you don't include the --volumes flag.

What characterizes a Docker image or container as active?

One can use the command docker system df (mirror) (introduced in Docker 1.13.0) 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
What does "active" mean? I.e., what characterizes an image or a container as active?
The Docker documentation on docker system df (mirror) doesn't explain it. The Docker glossary (mirror) doesn't contain the term "active".
I understand that:
"active" container means that the container is currently running (active containers can be listed with docker ps or equivalently docker container ls).
I believe it means:
Images: there is a container that exists using this image
Containers: container is currently running
Volumes: there is a container that exists with this volume mounted

Resources