Home Explore Blog CI



docker

1st chunk of `content/get-started/docker-concepts/building-images/understanding-image-layers.md`
a0979b92b4c20d8272130e0d9b9d3b1e3978b664e2c2c31b0000000100000fbf
---
title: Understanding the image layers
keywords: concepts, build, images, container, docker desktop
description: This concept page will teach you about the layers of container image.
summary: |
  Have you ever wondered how images work? This guide will help you to
  understand image layers - the fundamental building blocks of container
  images. You'll gain a comprehensive understanding of how layers are created,
  stacked, and utilized to ensure efficient and optimized containers.
weight: 1
aliases: 
 - /guides/docker-concepts/building-images/understanding-image-layers/
---

{{< youtube-embed wJwqtAkmtQA >}}

## Explanation

As you learned in [What is an image?](../the-basics/what-is-an-image/), container images are composed of layers. And each of these layers, once created, are immutable. But, what does that actually mean? And how are those layers used to create the filesystem a container can use?

### Image layers

Each layer in an image contains a set of filesystem changes - additions, deletions, or modifications. Let’s look at a theoretical image:

1. The first layer adds basic commands and a package manager, such as apt.
2. The second layer installs a Python runtime and pip for dependency management.
3. The third layer copies in an application’s specific requirements.txt file.
4. The fourth layer installs that application’s specific dependencies.
5. The fifth layer copies in the actual source code of the application.

This example might look like:

![screenshot of the flowchart showing the concept of the image layers](/Users/baehyunsol/Documents/Rust/ragit/sample/docker/content/get-started/docker-concepts/building-images/images/container_image_layers.webp?border=true)

This is beneficial because it allows layers to be reused between images. For example, imagine you wanted to create another Python application. Due to layering, you can leverage the same Python base. This will make builds faster and reduce the amount of storage and bandwidth required to distribute the images. The image layering might look similar to the following:

![screenshot of the flowchart showing the benefits of the image layering](/Users/baehyunsol/Documents/Rust/ragit/sample/docker/content/get-started/docker-concepts/building-images/images/container_image_layer_reuse.webp?border=true)

Layers let you extend images of others by reusing their base layers, allowing you to add only the data that your application needs.

### Stacking the layers

Layering is made possible by content-addressable storage and union filesystems. While this will get technical, here’s how it works:

1. After each layer is downloaded, it is extracted into its own directory on the host filesystem. 
2. When you run a container from an image, a union filesystem is created where layers are stacked on top of each other, creating a new and unified view.
3. When the container starts, its root directory is set to the location of this unified directory, using `chroot`.

When the union filesystem is created, in addition to the image layers, a directory is created specifically for the running container. This allows the container to make filesystem changes while allowing the original image layers to remain untouched. This enables you to run multiple containers from the same underlying image.

## Try it out

In this hands-on guide, you will create new image layers manually using the [`docker container commit`](https://docs.docker.com/reference/cli/docker/container/commit/) command. Note that you’ll rarely create images this way, as you’ll normally [use a Dockerfile](./writing-a-dockerfile.md). But, it makes it easier to understand how it’s all working.

### Create a base image

In this first step, you will create your own base image that you will then use for the following steps.

1. [Download and install](https://www.docker.com/products/docker-desktop/) Docker Desktop.


2. In a terminal, run the following command to start a new container:

    ```console
    $ docker run --name=base-container -ti ubuntu

Title: Understanding Container Image Layers
Summary
Container images are composed of immutable layers, each containing filesystem changes. Layers can be reused between images, speeding up builds and reducing storage/bandwidth. Content-addressable storage and union filesystems enable layering by stacking extracted layers to create a unified view. A separate directory is created for the running container, allowing filesystem changes without modifying the original image layers. The article guides you through manually creating image layers to understand the underlying mechanisms.