Back to course

Lesson 6: Understanding Docker Images and Layers

Docker Zero to Hero: The Complete Containerization Course for Beginners

Lesson 6: Understanding Docker Images and Layers

Containers are ephemeral, but images are the blueprints that make them reproducible. Let's dive into how images are constructed.

What is a Docker Image?

A Docker image is a read-only template that contains the operating system dependencies, libraries, and application code needed to run a piece of software. Images are built from a set of instructions defined in a Dockerfile.

The Layered Filesystem

Docker images are built using a stack of read-only layers. Each instruction in a Dockerfile creates a new layer.

Key Principles of Layers:

  1. Immutability: Once a layer is created, it cannot be changed.
  2. Sharing: If multiple images use the same base layers (e.g., the same version of Ubuntu), they only store that base layer once on disk, saving space.
  3. Efficiency: When you update an image, Docker only transfers the new or changed layers, not the entire image.

Containers as Layers

When a container is launched from an image, Docker adds a single, thin, writable layer on top of the image layers. This is called the Container Layer.

  • Any changes made inside the running container (writing logs, saving files) occur in this writable layer.
  • When the container is deleted, this writable layer is destroyed, meaning all changes are lost (unless explicitly saved or stored in volumes, which we cover later).

Listing and Inspecting Images

To see all images stored locally on your machine:

bash docker images

To get detailed information about a specific image, including its history and layers:

bash docker inspect nginx

To see the detailed history (showing the commands that built the layers):

bash docker history nginx

Understanding layers is the first step toward optimizing your images for speed and size.