Skip to main content

Working with Docker Images

Introduction

Docker images are the fundamental building blocks of containerized applications. An image is a lightweight, stand-alone, executable package that includes everything needed to run a piece of software: the code, runtime, libraries, environment variables, and configuration files. In this lesson, you'll learn how to find, pull, inspect, and manage Docker images, as well as understand their structure and common workflows.


What is a Docker Image?

A Docker image is an immutable snapshot of a filesystem. It contains the application and all its dependencies, but it is not a running process. When you start a container, Docker uses the image as the blueprint.

Key Characteristics:

  • Immutability: Once created, images do not change.
  • Layered Structure: Images are composed of layers, which makes sharing and caching efficient.
  • Portability: Images can be moved across environments (dev, staging, prod) with ease.

Finding and Pulling Docker Images

Docker images are typically stored on registries like Docker Hub or private registries.

Searching for Images

Use the docker search command to find images on Docker Hub.

docker search nginx

Sample output:

NAME          DESCRIPTION                STARS   OFFICIAL
nginx Official build of Nginx 17000 [OK]
jwilder/nginx-proxy Automated Nginx 2000

Pulling Images

To download an image from a registry:

docker pull nginx:latest
  • nginx is the image name.
  • latest is the tag (version). If omitted, latest is used by default.

Use Cases

  • Development: Quickly start a database or web server for local development.
  • CI/CD: Use predefined images for repeatable builds.

Listing and Inspecting Images

Listing Images

To list all images downloaded on your system:

docker images

Sample output:

REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
nginx latest 605c77e624dd 2 weeks ago 141MB

Inspecting Image Details

For detailed information about an image:

docker inspect nginx:latest

This command returns a JSON object with:

  • Environment variables
  • Exposed ports
  • Labels
  • Entry point and commands

Image Tags and Versioning

Tags are used to identify different versions of an image.

docker pull redis:7.0
docker pull redis:6.2
  • Always specify tags for production to avoid unexpected updates.

Common Pitfall:
Using the latest tag in production may lead to breaking changes when the image gets updated.


Removing Images

When you no longer need an image:

docker rmi nginx:latest

If the image is used by any container (even stopped), you must remove those containers first.


Saving and Loading Images

Saving an Image to a File

docker save -o nginx.tar nginx:latest

Use this to move images between air-gapped systems.

Loading an Image from a File

docker load -i nginx.tar

Common Pitfalls with Docker Images

  • Dangling Images: Images left after builds (<none> tag) consume disk space. Clean with:
    docker image prune
  • Large Images: Unnecessarily large images slow down deployments. Use minimal base images (e.g., alpine).
  • Not Specifying Tags: Leads to unpredictable builds and deployments.
  • Unused Images: Over time, unused images fill disk space. Regularly prune with:
    docker image prune -a

Real-World Use Cases

  • CI/CD Pipelines: Build images in CI, push to a registry, then pull to production.
  • Testing: Use specific versions of databases or services as images for integration testing.
  • Microservices: Each service can have its own custom image.

Summary

  • Docker images are the blueprints for containers.
  • Use docker pull, docker images, and docker inspect to work with images.
  • Always use specific tags in production.
  • Remove unused and dangling images to conserve disk space.
  • Images can be saved and transferred as files for offline distribution.

Quiz

1. What is the main difference between a Docker image and a Docker container?
A) An image is running, a container is not
B) An image is a template, a container is a running instance
C) Images can be changed, containers cannot
D) Images are always larger than containers

Answer: B


2. Which command will list all Docker images on your system?
A) docker ps
B) docker run
C) docker images
D) docker info

Answer: C


3. Why is it a bad practice to use the latest tag in production?
A) It uses more disk space
B) It may lead to unpredictable upgrades when the image is updated
C) It is slower to pull
D) It is not supported

Answer: B


4. How can you remove all dangling images from your system?
A) docker image prune
B) docker rmi --all
C) docker delete images
D) docker clean

Answer: A


5. What is the purpose of the docker save and docker load commands?
A) To backup and restore Docker containers
B) To export/import images as tar files for offline transfer
C) To update images
D) To build new images

Answer: B


Continue practicing with Docker images, as they are fundamental to efficient container-based development!