Common methods and problems of Docker cleaning

Common methods and problems of Docker cleaning

If you use docker for large-scale development but don't have a cleanup strategy, your disk will fill up quickly and when you really have to ship something immediately because the product is hot, you won't be able to ship it.

When we run a process in a computer, once the process is completed, everything is destroyed. Containers are the infrastructure that many of us operate on today. Everything runs in a container, aiming to have one process per container. When the process is complete, the container exits. But it won't clean up on its own.

What Docker has accumulated

You need to pay attention to these

  • Stopped container

  • Disk Volume

  • Mirror

  • network

You may not care too much about disk space if you have enough space, but network is important too. By default, Docker uses the bridge network, which has a limit of 31 networks. When the limit is reached, you will see the following message:

could not find an available, non-overlapping IPv4 address pool among the defaults to assign to the network

This can happen if you are a heavy docker-compose user who creates one network per project. You can fix the problem by setting a custom --subnet subnet, for example:

docker network create dada --subnet 192.167.11.0/24

But anyway, the point of this article is cleaning.

Clean up with docker

Cleaning up stopped containers

docker rm -v $(docker ps --all --quiet --filter 'status=exited')

This will find all containers in the exited state and print their IDs one per line so we can feed it to other shell commands.

We use docker rm -v to remove any anonymous volumes (volumes without an explicit name).

Cleaning up disk volumes

The above command should remove the volume associated with this container. If you created volumes manually and want to delete any unused volumes:

docker volume rm $(docker volume ls --quiet --filter 'dangling=true')

Clean up the image

It is usually safe to remove all Docker images. We can get it on demand when we need it. Usually after an image is cleaned, build times will be longer because the Docker daemon needs to spend time downloading the image again.

docker rm --force $(docker images --quiet)

Here, we use --force to force the removal of the image, even if a container is using that image. We can get this image later.

Cleaning up the network

It's very simple. We can delete any network and it will be recreated later on demand.

docker network rm $(docker network ls --quiet)

Clean up with docker-compose

If you use docker-compose to start your containers, we have an easy way to clean up the resources associated with a specific compose file.

docker-compose down --volumes --rmi all --remove-orphans

Unfortunately, this command does not remove anonymous volumes, so you will have to deal with those.

One command solves all problems

Docker is ephemeral, we can always re-obtain our image, re-create our database for development, or if this is just a continuous integration system, we can delete everything.

docker system prune --all --force --volumes

This concludes this article on common methods and issues of Docker cleanup. For more Docker cleanup content, please search 123WORDPRESS.COM’s previous articles or continue browsing the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Solution to Docker disk space cleaning
  • Docker cleanup environment operation
  • How to clean up the disk space occupied by Docker
  • How to regularly clean up docker private server images
  • How to clean up junk files generated by Docker
  • How to quickly clean up docker resources
  • Docker cleanup command collection
  • How to Completely Clean Your Docker Data

<<:  Analyze the difference between querySelector and getElementById methods in JavaScript

>>:  Eight rules for effective web forms

Recommend

The main idea of ​​​​dynamically setting routing permissions in Vue

I have seen some dynamic routing settings on the ...

Whitespace processing in HTML/CSS and how to preserve whitespace in the page

Whitespace rules in HTML In HTML, multiple spaces...

MySQL join buffer principle

Table of contents 1. MySQL join buffer 2. JoinBuf...

How to reduce memory usage and CPU usage of web pages

Some web pages may not look large but may be very...

The latest MySQL 5.7.23 installation and configuration graphic tutorial

The detailed installation and configuration of th...

Detailed explanation of how to use eslint in vue

Table of contents 1. Description 2. Download rela...

Docker implements re-tagging and deleting the image of the original tag

The docker image id is unique and can physically ...

How to install Chrome browser on CentOS 7

This article introduces how to install Chrome bro...

Bug of Chinese input garbled characters in flex program Firefox

Chinese characters cannot be input in lower versio...

ThingJS particle effects to achieve rain and snow effects with one click

Table of contents 1. Particle Effects 2. Load the...