Docker Images, Layers and Persistent Data

Image and container

An image is a reusable, normally immutable template. A container is an instance created from an image. The container receives a writable layer above the image layers.

Base image layers (read-only)
              + writable container layer
              = container filesystem view

When a container changes an existing file, copy-on-write creates a modified copy in the writable layer. Other containers can continue to use the original lower layer.

Why layers matter

Many containers can share the same base layers. This reduces disk use and speeds up later starts when the layers are already cached. A deleted file from a lower layer is represented in the union view by a deletion marker rather than physically changing the lower layer.

Persistence

The container’s writable layer is temporary. Removing the container normally removes those changes. Data that must survive belongs in a volume or mounted directory.

docker run -v /html:/usr/share/nginx/html nginx

This makes host directory /html available at the path where nginx expects website files. The mount is a storage connection; it is not an image rebuild.

Registry and tags

A registry stores images for download and sharing. A local Docker host caches pulled images. Image names may contain a registry, user, name and tag; a specific tag is preferable when reproducibility matters.

Retrieval

What is lost when a container is removed? Its writable container layer normally disappears. The image remains, and data stored in an external volume can remain.

Why do shared layers save space? Common base files are stored once and reused; only container-specific changes need additional storage.

Source: Lecture 07 - Lightweight Virtualization - LXC and Docker, PDF pages 35-47 and 50-52.