Prune Objects in Docker

Prune Objects in Docker

Pruning Objects in Docker

Pruning in Docker refers to the process of cleaning up unused Docker objects (such as containers, images, volumes, and networks) to free up system resources. Docker provides commands to prune unused or dangling resources that are no longer in use.

Types of Objects You Can Prune in Docker:

  1. Containers: Stopped containers that are not being used.

  2. Images: Unused images, including dangling images.

  3. Volumes: Unused volumes that are not connected to any containers.

  4. Networks: Unused networks not connected to any containers.

Docker Prune Commands

1. Prune Stopped Containers

To remove all stopped containers (containers that are no longer running):

docker container prune
  • This will prompt for confirmation before removing all stopped containers. If you want to skip the confirmation prompt, you can use the -f (force) flag:

    docker container prune -f

2. Prune Unused Docker Images

You can remove unused Docker images (images that are not tagged and are not used by any containers):

docker image prune
  • This will remove only dangling images (untagged images that are not used by any containers).

  • To remove all unused images (not just dangling), use the -a flag:

    docker image prune -a
  • Similar to other prune commands, you can use the -f flag to force the pruning without a prompt:

    docker image prune -a -f

3. Prune Unused Volumes

Docker volumes are used for persistent storage, but over time, unused volumes may accumulate. To remove unused volumes:

docker volume prune
  • This will remove all volumes that are not being used by any container. Use -f to skip the confirmation prompt:

    docker volume prune -f

4. Prune Unused Networks

If there are unused networks (networks that are not connected to any containers), you can remove them:

docker network prune
  • Use the -f flag to skip the confirmation prompt:

    docker network prune -f

5. Prune All Unused Objects

You can remove all unused Docker objects (containers, images, volumes, and networks) with a single command:

docker system prune
  • This will remove:

    • Stopped containers

    • Unused networks

    • Dangling images

    • Unused volumes (with the --volumes flag)

  • To include unused volumes in the prune operation, use the --volumes flag:

    docker system prune --volumes
  • If you want to skip the confirmation prompt, use the -f flag:

    docker system prune -f --volumes
  • To also remove unused images (not just dangling ones), use the -a flag:

    docker system prune -a -f --volumes

This will clean up almost everything that is not being used, giving you a fresh environment.

Summary of Docker Prune Commands

  • Prune Stopped Containers:

    docker container prune
  • Prune Unused Images:

    docker image prune
  • Prune All Unused Images:

    docker image prune -a
  • Prune Unused Volumes:

    docker volume prune
  • Prune Unused Networks:

    docker network prune
  • Prune All Unused Objects (Containers, Images, Volumes, Networks):

    docker system prune
  • Prune All Unused Objects and Volumes:

    docker system prune --volumes
  • Prune All Unused Objects, Including All Images and Volumes:

    docker system prune -a -f --volumes

Best Practices

  • Prune Regularly: Prune unused Docker objects regularly to free up system resources.

  • Careful with -a flag: Be cautious when using the -a flag with pruning commands (like docker image prune -a), as it will remove all unused images, even if they are not dangling.

  • Avoid Removing Volumes: Volumes store persistent data. Be careful when pruning volumes with docker volume prune or docker system prune --volumes, as you might delete important data.

Souy Soeng

Souy Soeng

Hi there 👋, I’m Soeng Souy (StarCode Kh)
-------------------------------------------
🌱 I’m currently creating a sample Laravel and React Vue Livewire
👯 I’m looking to collaborate on open-source PHP & JavaScript projects
💬 Ask me about Laravel, MySQL, or Flutter
⚡ Fun fact: I love turning ☕️ into code!

1 Comments

CAN FEEDBACK
  1. Anonymous
    Anonymous
    any tut for that
close