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.
Remove images and containers
- Remove an image:
docker rmi <image>(for exampledocker rmi alpine:latest). - Remove a container:
docker rm <container>.
alpine:latest:
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:docker ps— shows only running containers.docker ps -a— shows all containers, including exited ones that still reference images.
292efbf123b0 still references the nginx image, remove the container first, then remove the image:
nginx image:
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
0B:
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.