Skip to main content
In this lesson we cover Docker images and how to clean them up safely. You’ll learn how images are pulled, where to see what is stored locally, how to remove images and containers, and how to prune unused data to reclaim disk space.

How docker run pulls and starts images

When you run a container with a command like docker run nginx, Docker will first pull the nginx image (if it’s not already present) and then start a container from that image. The container’s startup logs are printed to the terminal. Example output from running docker run nginx:

List images stored locally

To inspect images on the local host, use:
  • docker images — list all images stored locally.
Example output:

Remove images and containers

  • Remove an image: docker rmi <image> (for example docker rmi alpine:latest).
  • Remove a container: docker rm <container>.
Example: removing alpine:latest:
After removing alpine, the image list shows only nginx:

Image in use: what happens if a container references the image

If a container (running or stopped) still references an image, Docker will refuse to delete that image and report a conflict:
To determine why, check your containers:
  • docker ps — shows only running containers.
  • docker ps -a — shows all containers, including exited ones that still reference images.
Example:
Because the exited container 292efbf123b0 still references the nginx image, remove the container first, then remove the image:
Now you can remove the nginx image:
After cleaning up, docker ps, docker ps -a, and docker images report nothing relevant:

Bulk cleanup with docker system prune

If you have many stopped containers and dangling or unused images, docker system prune is a convenient interactive command to reclaim space. It removes:
  • all stopped containers
  • all networks not used by at least one container
  • all dangling images
  • the unused build cache
Run it interactively:
If there is nothing to remove, the command reports reclaimed space as 0B:
Table: quick reference for cleanup options
Using docker system prune -a or adding --volumes can delete images and data that you expect to keep. Always double-check which containers and volumes you still need before running these commands.
If you want to remove all unused images (not just dangling ones), run docker system prune -a. That will remove all images not referenced by any container. Use it with caution.

Watch Video

Practice Lab