Skip to content

03. Docker Images

Docker images are the fundamental building blocks of containerization. They contain the application code, runtime, libraries, environment variables, and configuration files needed to run a container. If a container is a running instance, the image is the blueprint.

A Docker image is a read-only, executable package that includes everything needed to run a piece of software. It is essentially a snapshot of a filesystem and application configuration.

Images are built using a layered filesystem (typically overlay2). Instead of storing the entire filesystem as one massive block, Docker divides it into separate, stackable layers.

  • If multiple images use the same base layer (like Ubuntu), Docker only downloads and stores that layer once, sharing it across all images. This massively reduces disk usage and speeds up downloads.

Every instruction in a Dockerfile (like RUN apt-get install or COPY . /app) creates a new layer on top of the previous one. Once built, these layers are immutable (read-only). They can never be changed, which guarantees consistency.

When you run a container from an image, Docker mounts a thin read-write layer on top of the stack of read-only image layers. Any files modified, created, or deleted during the container’s lifecycle happen in this top writable layer. When the container is deleted, this layer is destroyed.

Every image is uniquely identified by a 64-character hexadecimal ID, generated by hashing the contents of the image.

Because long hexadecimal IDs are impossible to remember, images are assigned human-readable tags (e.g., nginx:latest, postgres:14).

A digest is the immutable, cryptographic SHA256 hash of the image. While a tag like latest can be overwritten to point to a new image, a digest never changes.

These are the primary CLI commands used to manage images on your host.

Lists all images currently downloaded and stored on your local machine.

Terminal window
docker images

Downloads an image from a registry (like Docker Hub) to your local machine without running it.

Terminal window
docker pull redis:alpine

Uploads your locally built image to a remote registry so others (or your servers) can pull it.

Terminal window
docker push myuser/myapp:1.0

Builds a new Docker image from a Dockerfile.

Terminal window
# Builds the current directory (.) and tags it as myapp:latest
docker build -t myapp:latest .

Removes an image from your local machine. You cannot remove an image if a container (even a stopped one) is currently using it.

Terminal window
docker rmi redis:alpine

Creates a new tag (alias) that refers to an existing image. This does not copy the image; it just adds a new label pointing to the same Image ID.

Terminal window
docker tag myapp:latest myuser/myapp:v1.0

Shows the history of an image, detailing every layer that was created during the build process and how much disk space each layer consumes.

Terminal window
docker history nginx:latest

Used for air-gapped environments or manual transfers. save exports an image to a .tar archive file, and load imports it.

Terminal window
# Export the image to a tarball
docker save -o myapp.tar myapp:latest
# Import the image from the tarball on a different machine
docker load -i myapp.tar

Understanding the anatomy of a Docker image name is critical for interacting with registries.

The full structure is:

registry/namespace/image:tag

Example:

Terminal window
docker.io/myuser/myapp:1.0
  • Registry: The server hosting the images (e.g., docker.io for Docker Hub, ghcr.io for GitHub, gcr.io for Google Cloud). If omitted, Docker defaults to docker.io.
  • Namespace (or User): The account or organization name (e.g., myuser). Official Docker Hub images (like nginx or ubuntu) have no namespace.
  • Image (or Repository): The actual name of the software (e.g., myapp).
  • Tag: The version identifier (e.g., 1.0). If omitted, Docker defaults to the tag :latest.

When pulling images for production, how you reference the image heavily impacts stability and security.

By default, if you don’t specify a tag (e.g., docker pull nginx), Docker implicitly pulls nginx:latest.

  • Warning: latest is just a standard string. It does not automatically mean “the newest version”. It simply means the image was explicitly tagged with the word latest by the maintainer. Never use :latest in production, as updates can break your app unexpectedly.

Always specify exact version tags to ensure reproducible deployments. Most serious projects use Semantic Versioning (SemVer) consisting of MAJOR.MINOR.PATCH.

  • Example: node:18.17.0 (Highly specific, very safe)
  • Example: node:18 (Less specific, updates to the newest 18.x minor version)

A tag can be overwritten (someone could push a new, broken image and tag it 1.0). If you need absolutely 100% guaranteed immutability, you can pull an image by its cryptographic digest.

Pulling by digest ensures that you receive the exact same bits every single time, even if the tag is maliciously altered or deleted.

Terminal window
docker pull nginx@sha256:4c0fdaa8b6241a788c...