Skip to content

Instantly share code, notes, and snippets.

@mika
Created June 25, 2020 12:03
Show Gist options
  • Save mika/fbba4d5c443ca17d8881442b2361e526 to your computer and use it in GitHub Desktop.
Save mika/fbba4d5c443ca17d8881442b2361e526 to your computer and use it in GitHub Desktop.
Docker cleanup

DOCS: also refer to https://docs.docker.com/config/pruning/

HINT: add the --force option to the cmdlines to not prompt for confirmation (e.g. for usage inside scripts)

Get rid of all stopped containers (list via docker ps --filter status=exited):

% docker container prune

Get rid of all stopped container older than 12 hours:

% docker container prune --filter "until=12h"

Get rid of dangling images (list via docker image ls --filter dangling=true):

% docker image prune

Remove all unused images (dangling images and images without at least one container associated to them (intermediate ones), list via docker image ls --filter dangling=true -a):

% docker image prune -a

Remove all unused images, older than 14 days:

% docker image prune --filter until=336h

Remove all stopped containers, networks not used by at least one container, dangling images and dangling build cache (note: docker <=17.06.0 also prunes volumes here):

% docker system prune

Remove all stopped containers, networks not used by at least one container, images without at least one container associated to them and all build cache (note: docker <=17.06.0 also prunes volumes here):

% docker system prune -a

Remove all stopped containers, networks not used by at least one container, dangling images and dangling build cache, older than 14 days (note: docker <=17.06.0 also prunes volumes here):

% docker system prune --filter until=336h

Remove all stopped containers, networks not used by at least one container, dangling images, dangling build cache and volumes not used by at least one container (requires docker >=17.06.1):

% docker system prune --volumes

Remove all local volumes not used by at least one container:

% docker volume prune

Remove dangling volumes (list via docker volume ls --filter dangling=true):

% docker volume prune --filter dangling=true

Remove all networks not used by at least one container:

% docker network prune

Remove all networks not used by at least one container, older than 12 hours:

% docker network prune --filter until=12h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment