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

How to enable Swoole Loader extension on Linux system virtual host

Special note: Only the Swoole extension is instal...

How can MySQL effectively prevent database deletion and running away?

Table of contents Safe Mode Settings test 1. Upda...

How to use MyCat to implement MySQL master-slave read-write separation in Linux

Table of contents Linux-Use MyCat to implement My...

How to use React slots

Table of contents need Core Idea Two ways to impl...

What to do if you forget your password in MySQL 5.7.17

1. Add skip-grant-tables to the my.ini file and r...

Detailed explanation of samba folder sharing server configuration under centos

1. Introduction Recently I found that there are m...

Dynamic starry sky background implemented with CSS3

Result:Implementation Code html <link href=...

Introducing the code checking tool stylelint to share practical experience

Table of contents Preface text 1. Install styleli...

CSS3 realizes text relief effect, engraving effect, flame text

To achieve this effect, you must first know a pro...

Example of using CSS3 to achieve shiny font effect when unlocking an Apple phone

0. Introduction August 18, 2016 Today, I noticed ...