A Beginner-Friendly Guide to Docker Images – With Full Command Breakdown

Author

Kritim Yantra

Apr 24, 2025

A Beginner-Friendly Guide to Docker Images – With Full Command Breakdown

Docker is a powerful platform that allows developers to build, ship, and run applications in lightweight, portable containers. At the heart of Docker lies the concept of Docker Images, which are the blueprints for creating containers.

In this blog, we’ll explore:

  • What is a Docker Image?
  • Key Components of a Docker Image
  • Common Docker Image Commands (with detailed explanations)
  • Viewing and Filtering Local Images
  • Best Practices for Managing Docker Images

What is a Docker Image?

A Docker Image is a read-only template that contains instructions for creating a Docker container. It includes:

  • The application code
  • Runtime environment (e.g., Node.js, Python, Java)
  • System libraries & dependencies
  • Configuration files

Images are built using a Dockerfile, which defines all the necessary steps to assemble the image.


Key Components of a Docker Image

  1. Base Image – The starting point (e.g., ubuntu, alpine, python:3.9).
  2. Layers – Each instruction in a Dockerfile adds a new layer to the image.
  3. Image ID – A unique SHA-256 hash identifier.
  4. Tags – Labels used to version images (e.g., nginx:latest, python:3.9-slim).

Common Docker Image Commands (Explained in Detail)

1. docker pull – Download an Image from a Registry

Downloads an image from Docker Hub (or another registry) to your local machine.

Syntax:

docker pull [OPTIONS] IMAGE_NAME[:TAG]

Example:

docker pull nginx:latest

Explanation:

  • nginx:latest → Downloads the latest version of the Nginx image.
  • If no tag is specified, Docker uses latest by default.

2. docker images / docker image ls – List Downloaded Images

Displays all locally stored Docker images with various filtering and formatting options.

Syntax:

docker images [OPTIONS] [REPOSITORY[:TAG]]
# OR
docker image ls [OPTIONS] [REPOSITORY[:TAG]]

Example:

docker images

Output:

REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
nginx        latest    abc12345678    2 weeks ago    133MB
ubuntu       20.04     def98765432    3 weeks ago    72.8MB

Filtering and Formatting Options

  • --filter (-f) → Filter images based on criteria:

    • reference="nginx*" → Shows images with names starting with "nginx".
    • before=nginx:latest → Lists images created before nginx:latest.
    • since=ubuntu:20.04 → Lists images created after ubuntu:20.04.
    • dangling=true → Shows untagged (dangling) images.
    • label=version=1.0 → Filters images with a specific label.
  • --all (-a) → Shows all images (including intermediate layers).

  • --no-trunc → Displays full image IDs (without truncation).

  • --quiet (-q) → Only shows image IDs (useful for scripting).

  • --digests → Displays SHA-256 digests (for verification).

Examples:

# Show only dangling (untagged) images
docker images --filter dangling=true

# Display images created before a specific image
docker images --filter before=nginx:latest

# Show images matching a reference pattern
docker images --filter reference="nginx*"

# List only image IDs (useful for automation)
docker images -q

3. docker build – Create an Image from a Dockerfile

Builds a new Docker image using instructions from a Dockerfile.

Syntax:

docker build [OPTIONS] PATH

Example:

docker build -t my-app:1.0 .

Explanation:

  • -t my-app:1.0 → Tags the image with a name (my-app) and version (1.0).
  • . → The build context (current directory where the Dockerfile is located).

4. docker rmi – Remove an Image

Deletes a locally stored Docker image.

Syntax:

docker rmi [OPTIONS] IMAGE [IMAGE...]

Example:

docker rmi nginx:latest

Explanation:

  • Removes the specified image from your system.
  • If a container is using the image, you must first remove the container.

5. docker tag – Tag an Image

Assigns a new tag to an existing image (useful for versioning).

Syntax:

docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

Example:

docker tag my-app:1.0 my-app:latest

Explanation:

  • Creates a new reference (my-app:latest) pointing to the same image as my-app:1.0.
  • Helps manage different versions of an image.

6. docker push – Upload an Image to a Registry

Uploads a locally built image to a registry (e.g., Docker Hub).

Syntax:

docker push [OPTIONS] NAME[:TAG]

Example:

docker push my-app:1.0

Explanation:

  • Requires you to be logged in (docker login).
  • Pushes the image to a remote repository (e.g., Docker Hub, AWS ECR).

7. docker save – Export an Image to a File

Saves an image as a .tar file for sharing or backup.

Syntax:

docker save -o FILENAME.tar IMAGE_NAME

Example:

docker save -o nginx.tar nginx:latest

Explanation:

  • Creates a compressed .tar file of the image.
  • Useful for offline transfer.

8. docker load – Import an Image from a File

Loads an image from a .tar file.

Syntax:

docker load -i FILENAME.tar

Example:

docker load -i nginx.tar

Explanation:

  • Restores an image that was previously saved with docker save.

Best Practices for Managing Docker Images

  1. Use Specific Tags → Avoid latest in production; use versioned tags (python:3.9).
  2. Keep Images Small → Use lightweight base images like alpine.
  3. Remove Unused Images → Run docker image prune to clean up.
  4. Scan for Vulnerabilities → Use docker scan to check for security issues.
  5. Multi-Stage Builds → Reduce final image size by discarding unnecessary layers.

Conclusion

Docker images are the foundation of containerized applications. Understanding how to pull, build, tag, and manage them is crucial for efficient Docker usage.

Quick Recap

Command Purpose
docker pull Download an image
docker images / docker image ls List local images (with filtering)
docker build Build an image from a Dockerfile
docker rmi Remove an image
docker tag Assign a new tag
docker push Upload to a registry
docker save Export as .tar
docker load Import from .tar

By mastering these commands and filtering options, you'll be able to efficiently manage Docker images in your projects! 🚀

Happy Dockering! 🐳

Tags

Comments

No comments yet. Be the first to comment!

Please log in to post a comment:

Sign in with Google

Related Posts