Loading...

How to use docker system prune

Jonas Scholz
3 min read 12. Sept. 2024

Let's talk about a handy Docker command called docker system prune. This command helps you clean up your Docker system by removing things you're not using anymore. It's like tidying up your room, but for Docker!

To use the command, you'll type docker system prune followed by any options you want to use. Here's how it works and what you can do with it:

What Does It Do?

When you run docker system prune, it gets rid of:

  • Unused containers: These are containers that aren't running and aren't being used.
  • Unused networks: Networks that aren't connected to any containers.
  • Unused images: Both dangling images (images that are not tagged and not referenced by any container) and unused images that you're not actively using.

By default, it won't touch your volumes. Volumes are where your data is stored, and Docker is careful not to delete them accidentally. If you want to clean up volumes too, you can use an option we'll talk about later.

Basic Usage

To run the command, you just need to open your terminal and type:

docker system prune

This will start the cleaning process. Docker will ask if you're sure you want to proceed, so you'll need to type 'y' and hit enter to confirm.

Options You Can Use

There are a few options you can add to the command to change how it works:

  1. -a or --all: This tells Docker to remove all unused images, not just the dangling ones. So, if you have images you're not using at all, this will get rid of them.
    docker system prune -a
    
  2. --filter: This option lets you be more specific about what you want to remove. You can use filters like until and label.
    • until: This filter removes items created before a certain time. You can use timestamps or duration strings. For example, to remove items created more than an hour ago:
      docker system prune --filter "until=1h"
      
    • label: This filter removes items based on labels. You can use it to keep or remove items with specific labels. For example, to remove items without the label keep:
      docker system prune --filter "label!=keep"
      
  3. -f or --force: If you don't want to be prompted for confirmation, you can use this option. It'll run the command without asking if you're sure.
    docker system prune -f
    
  4. --volumes: This option tells Docker to remove anonymous volumes, which are volumes not explicitly created by the user. Be careful with this one, as it can delete data!
    docker system prune --volumes
    

Example

Let's say you've been working on a project and you've created a bunch of test containers, networks, and images. You've finished the project and want to clean up. You can use the following command to remove everything you're not using anymore, including volumes:

docker system prune -a --volumes

This will remove all unused containers, networks, images, and anonymous volumes. Just remember to be careful with the --volumes option, as it can delete data you might want to keep.

Final Thoughts

Using docker system prune is a great way to keep your Docker system tidy. Just make sure you understand what you're removing, especially when using options like --volumes. If you're ever unsure, you can always run the command without the -f option to see what would be removed before confirming.

Welcome to the container cloud

Sliplane makes it simple to deploy containers in the cloud and scale up as you grow. Try it now and get started in minutes!