Here’s a comprehensive list of Docker commands related to images, broken down by category, with explanations and examples:
1. Pulling and Searching Images
Command | Purpose | Example |
---|
docker pull <image> | Download an image from a registry (e.g., Docker Hub) | docker pull nginx:latest |
docker search <image> | Search for an image in Docker Hub | docker search postgres |
2. Listing Images
Command | Purpose | Example |
---|
docker images or docker image ls | List all local images | docker images |
docker image ls -a | List all images including intermediate layers | docker image ls -a |
docker image ls --filter "dangling=true" | List dangling (untagged) images | docker image ls --filter "dangling=true" |
3. Building Images
Command | Purpose | Example |
---|
docker build -t <name>:<tag> <path> | Build an image from a Dockerfile | docker build -t myapp:1.0 . |
docker build --file <Dockerfile> | Specify a Dockerfile with a different name | docker build -t myapp:1.0 -f Dockerfile.dev . |
docker build --no-cache | Build an image without using cache | docker build --no-cache -t myapp:1.0 . |
docker build --pull | Always attempt to pull a newer version of the base image | docker build --pull -t myapp:1.0 . |
4. Tagging Images
Command | Purpose | Example |
---|
docker tag <source> <target> | Add a tag to an existing image | docker tag myapp:1.0 myrepo/myapp:1.0 |
5. Pushing Images
Command | Purpose | Example |
---|
docker push <repository>:<tag> | Push an image to a registry | docker push myrepo/myapp:1.0 |
6. Inspecting Images
Command | Purpose | Example |
---|
docker inspect <image> | Show detailed info about an image | docker inspect nginx:latest |
docker history <image> | Show image layer history | docker history myapp:1.0 |
docker image inspect <image> | Same as docker inspect but specifically for images | docker image inspect myapp:1.0 |
7. Removing Images
Command | Purpose | Example |
---|
docker rmi <image> | Remove a specific image | docker rmi myapp:1.0 |
docker image prune | Remove dangling images (untagged) | docker image prune |
docker image prune -a | Remove all unused images | docker image prune -a |
8. Saving & Loading Images
Command | Purpose | Example |
---|
docker save -o <file.tar> <image> | Save an image to a tar file | docker save -o myapp.tar myapp:1.0 |
docker load -i <file.tar> | Load an image from a tar file | docker load -i myapp.tar |
9. Other Useful Image Commands
Command | Purpose | Example |
---|
docker image ls --format "{{.Repository}}:{{.Tag}}" | List images in a custom format | docker image ls --format "{{.Repository}}:{{.Tag}}" |
docker image prune --filter "label=<key>=<value>" | Remove images based on labels | docker image prune --filter "label=stage=dev" |
✅ Tips for Working with Docker Images
- Use tags to version your images clearly (
:latest
is not always best for production).
- Clean up unused images regularly to save disk space (
docker image prune -a
).
- Use small base images like Alpine to reduce image size.
- Inspect layers (
docker history
) to understand how each instruction affects size.
- Multi-stage builds help reduce final image size while keeping Dockerfile maintainable.