CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Docker / docker system prune

docker system prune

Reclaim disk space from unused Docker data

Intermediatecleanupdiskoperations

docker system prune deletes what nothing is using: stopped containers, unused networks, dangling images and build cache, and with flags, every unused image and anonymous volumes. It's the standard fix for a Docker host or CI runner whose /var/lib/docker has filled the disk, and it's destructive if you add flags without thinking.

Syntaxdocker system prune [-a] [--volumes] [--filter until=24h] [-f]

Watch it run

Disk alert on a CI runner: / is 94% full and /var/lib/docker is the culprit. Measure before deleting anything.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
df -h /var/lib/docker

Docker's data lives on the root filesystem here, so when it fills, everything on the box suffers.

Anatomy of a command

$
commanddocker system prune

Remove unused containers, networks, images and build cache in one go.

How it works

prune is several API calls in a row: POST /containers/prune, /networks/prune, /images/prune and /build/prune (plus /volumes/prune with --volumes), each with the same filters. 'Unused' is decided by references: a container is removable if it isn't running; a network if no container is attached; an image if no container, running or stopped, was created from it (dangling-only without -a); a volume if no container mounts it. Since Docker 23, volume pruning removes only anonymous volumes unless you also ask for all. Images are removed by deleting their tags and then any layers no other image shares, so the space freed is often less than the image sizes suggest. Running containers, and the images, networks and volumes they use, are never touched.

Options you'll actually use

-a, --allRemove all unused images, not only dangling ones
--volumesAlso remove unused anonymous volumes
--filter until=24hOnly objects older than this
--filter label=env=ciOnly objects with (or label!= without) a label
-f, --forceSkip the confirmation prompt
docker system df [-v]Show what uses space and what's reclaimable first
<type> prunePrune one kind only: image, container, volume, network or builder

Common mistakes

docker system prune -a --volumes -f on a production host

Removes every image not in use (slow restarts while they're re-pulled) and anonymous volumes, which may hold real data.

docker system prune (defaults), then targeted image/volume prunes after checking docker system df -v
rm -rf /var/lib/docker/overlay2/... to free space

Deleting Docker's files behind its back corrupts images and containers; dockerd still references them.

Use docker prune commands, or stop dockerd and wipe /var/lib/docker entirely if you really want a reset
Pruning to fix a disk that's full because of container logs

Running containers aren't pruned, and their JSON logs keep growing.

Check /var/lib/docker/containers/*/*-json.log and configure log rotation (see docker logs)

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Show Docker's disk usage per image, container and volume on this host.

  2. 2

    In the sandbox, do the first step of every disk investigation: show filesystem usage and the five biggest directories under /var/log.

  3. 3

    Write a non-interactive prune for a CI runner that removes unused images and build cache older than 3 days but leaves volumes alone.

Related commands

docker imagesList, filter and clean up local imagesdocker volumeManage persistent data for containersdocker logsRead a container's stdout and stderrdfReport used and free space per filesystemduFind out which files and directories use the space
Previousdocker volumeNextdocker compose
Linux / Docker / docker system prune

docker system prune

Reclaim disk space from unused Docker data

Intermediatecleanupdiskoperations

docker system prune deletes what nothing is using: stopped containers, unused networks, dangling images and build cache, and with flags, every unused image and anonymous volumes. It's the standard fix for a Docker host or CI runner whose /var/lib/docker has filled the disk, and it's destructive if you add flags without thinking.

Syntaxdocker system prune [-a] [--volumes] [--filter until=24h] [-f]

Watch it run

Disk alert on a CI runner: / is 94% full and /var/lib/docker is the culprit. Measure before deleting anything.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
df -h /var/lib/docker

Docker's data lives on the root filesystem here, so when it fills, everything on the box suffers.

Anatomy of a command

$
commanddocker system prune

Remove unused containers, networks, images and build cache in one go.

How it works

prune is several API calls in a row: POST /containers/prune, /networks/prune, /images/prune and /build/prune (plus /volumes/prune with --volumes), each with the same filters. 'Unused' is decided by references: a container is removable if it isn't running; a network if no container is attached; an image if no container, running or stopped, was created from it (dangling-only without -a); a volume if no container mounts it. Since Docker 23, volume pruning removes only anonymous volumes unless you also ask for all. Images are removed by deleting their tags and then any layers no other image shares, so the space freed is often less than the image sizes suggest. Running containers, and the images, networks and volumes they use, are never touched.

Options you'll actually use

-a, --allRemove all unused images, not only dangling ones
--volumesAlso remove unused anonymous volumes
--filter until=24hOnly objects older than this
--filter label=env=ciOnly objects with (or label!= without) a label
-f, --forceSkip the confirmation prompt
docker system df [-v]Show what uses space and what's reclaimable first
<type> prunePrune one kind only: image, container, volume, network or builder

Common mistakes

docker system prune -a --volumes -f on a production host

Removes every image not in use (slow restarts while they're re-pulled) and anonymous volumes, which may hold real data.

docker system prune (defaults), then targeted image/volume prunes after checking docker system df -v
rm -rf /var/lib/docker/overlay2/... to free space

Deleting Docker's files behind its back corrupts images and containers; dockerd still references them.

Use docker prune commands, or stop dockerd and wipe /var/lib/docker entirely if you really want a reset
Pruning to fix a disk that's full because of container logs

Running containers aren't pruned, and their JSON logs keep growing.

Check /var/lib/docker/containers/*/*-json.log and configure log rotation (see docker logs)

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Show Docker's disk usage per image, container and volume on this host.

  2. 2

    In the sandbox, do the first step of every disk investigation: show filesystem usage and the five biggest directories under /var/log.

  3. 3

    Write a non-interactive prune for a CI runner that removes unused images and build cache older than 3 days but leaves volumes alone.

Related commands

docker imagesList, filter and clean up local imagesdocker volumeManage persistent data for containersdocker logsRead a container's stdout and stderrdfReport used and free space per filesystemduFind out which files and directories use the space
Previousdocker volumeNextdocker compose