Skip to main content
In this lesson we cover managing Docker images and removing unused containers and images so your host stays clean and predictable. When you run a container from an image (for example nginx), Docker will pull the image if it isn’t already present, then start a container. Example output from docker run nginx (abridged):
Viewing local images
  • List images that are stored locally with docker images. Example output:
Removing images and containers
  • docker rmi IMAGE — remove one or more images.
  • docker rm CONTAINER — remove one or more containers.
Example: removing the small alpine:latest image:
After removing alpine, verify remaining images:
Handling conflicts when removing images If an image is referenced by a container (running or stopped), docker rmi will fail with a conflict error:
That means at least one container still references the image. Use docker ps and docker ps -a to locate such containers:
  • docker ps — lists running containers
  • docker ps -a — lists all containers, including exited/stopped ones
Example showing no running containers but one exited container referencing the image:
Remove the stopped container (you can use a prefix of the container ID):
Confirm there are no containers referencing the image, then remove the image:
Now docker ps, docker ps -a, and docker images should show no entries for those containers/images. Quick command reference Cleaning up many unused resources When you have many images and containers while experimenting, docker system prune is a convenient way to remove unused resources. It prompts for confirmation and removes:
  • all stopped containers
  • all networks not used by at least one container
  • all dangling images (untagged)
  • unused build cache
Example prompt:
Be careful with docker system prune. It permanently deletes stopped containers, dangling images, and other unused resources. If you need to preserve any stopped container or image, do not prune.
Best practices and further reading
  • Inspect containers before removing them to avoid losing important state (use docker inspect <container>).
  • If you need to force-remove an image despite references, use docker rmi -f <image> — but prefer removing or exporting containers first.
  • For more details, see the official Docker docs: https://docs.docker.com/engine/reference/commandline/cli/
Use these commands and checks to keep your development host tidy while experimenting with containers and images.

Watch Video

Practice Lab