Docker images

Here’s a comprehensive list of Docker commands related to images, broken down by category, with explanations and examples:


1. Pulling and Searching Images

CommandPurposeExample
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 Hubdocker search postgres

2. Listing Images

CommandPurposeExample
docker images or docker image lsList all local imagesdocker images
docker image ls -aList all images including intermediate layersdocker image ls -a
docker image ls --filter "dangling=true"List dangling (untagged) imagesdocker image ls --filter "dangling=true"

3. Building Images

CommandPurposeExample
docker build -t <name>:<tag> <path>Build an image from a Dockerfiledocker build -t myapp:1.0 .
docker build --file <Dockerfile>Specify a Dockerfile with a different namedocker build -t myapp:1.0 -f Dockerfile.dev .
docker build --no-cacheBuild an image without using cachedocker build --no-cache -t myapp:1.0 .
docker build --pullAlways attempt to pull a newer version of the base imagedocker build --pull -t myapp:1.0 .

4. Tagging Images

CommandPurposeExample
docker tag <source> <target>Add a tag to an existing imagedocker tag myapp:1.0 myrepo/myapp:1.0

5. Pushing Images

CommandPurposeExample
docker push <repository>:<tag>Push an image to a registrydocker push myrepo/myapp:1.0

6. Inspecting Images

CommandPurposeExample
docker inspect <image>Show detailed info about an imagedocker inspect nginx:latest
docker history <image>Show image layer historydocker history myapp:1.0
docker image inspect <image>Same as docker inspect but specifically for imagesdocker image inspect myapp:1.0

7. Removing Images

CommandPurposeExample
docker rmi <image>Remove a specific imagedocker rmi myapp:1.0
docker image pruneRemove dangling images (untagged)docker image prune
docker image prune -aRemove all unused imagesdocker image prune -a

8. Saving & Loading Images

CommandPurposeExample
docker save -o <file.tar> <image>Save an image to a tar filedocker save -o myapp.tar myapp:1.0
docker load -i <file.tar>Load an image from a tar filedocker load -i myapp.tar

9. Other Useful Image Commands

CommandPurposeExample
docker image ls --format "{{.Repository}}:{{.Tag}}"List images in a custom formatdocker image ls --format "{{.Repository}}:{{.Tag}}"
docker image prune --filter "label=<key>=<value>"Remove images based on labelsdocker image prune --filter "label=stage=dev"

Tips for Working with Docker Images

  1. Use tags to version your images clearly (:latest is not always best for production).
  2. Clean up unused images regularly to save disk space (docker image prune -a).
  3. Use small base images like Alpine to reduce image size.
  4. Inspect layers (docker history) to understand how each instruction affects size.
  5. Multi-stage builds help reduce final image size while keeping Dockerfile maintainable.

Leave a Reply