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.
3.1 Image Fundamentals
Section titled “3.1 Image Fundamentals”What is an Image?
Section titled “What is an Image?”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.
Image Layers
Section titled “Image Layers”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.
Read-Only Layers
Section titled “Read-Only Layers”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.
Container Writable Layer
Section titled “Container Writable Layer”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.
Image IDs
Section titled “Image IDs”Every image is uniquely identified by a 64-character hexadecimal ID, generated by hashing the contents of the image.
Image Tags
Section titled “Image Tags”Because long hexadecimal IDs are impossible to remember, images are assigned human-readable tags (e.g., nginx:latest, postgres:14).
Image Digests
Section titled “Image Digests”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.
3.2 Image Commands
Section titled “3.2 Image Commands”These are the primary CLI commands used to manage images on your host.
docker images (or docker image ls)
Section titled “docker images (or docker image ls)”Lists all images currently downloaded and stored on your local machine.
docker imagesdocker pull
Section titled “docker pull”Downloads an image from a registry (like Docker Hub) to your local machine without running it.
docker pull redis:alpinedocker push
Section titled “docker push”Uploads your locally built image to a remote registry so others (or your servers) can pull it.
docker push myuser/myapp:1.0docker build
Section titled “docker build”Builds a new Docker image from a Dockerfile.
# Builds the current directory (.) and tags it as myapp:latestdocker build -t myapp:latest .docker rmi (or docker image rm)
Section titled “docker rmi (or docker image rm)”Removes an image from your local machine. You cannot remove an image if a container (even a stopped one) is currently using it.
docker rmi redis:alpinedocker tag
Section titled “docker tag”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.
docker tag myapp:latest myuser/myapp:v1.0docker history
Section titled “docker history”Shows the history of an image, detailing every layer that was created during the build process and how much disk space each layer consumes.
docker history nginx:latestdocker save & docker load
Section titled “docker save & docker load”Used for air-gapped environments or manual transfers. save exports an image to a .tar archive file, and load imports it.
# Export the image to a tarballdocker save -o myapp.tar myapp:latest
# Import the image from the tarball on a different machinedocker load -i myapp.tar3.3 Image Naming
Section titled “3.3 Image Naming”Understanding the anatomy of a Docker image name is critical for interacting with registries.
The full structure is:
registry/namespace/image:tagExample:
docker.io/myuser/myapp:1.0- Registry: The server hosting the images (e.g.,
docker.iofor Docker Hub,ghcr.iofor GitHub,gcr.iofor Google Cloud). If omitted, Docker defaults todocker.io. - Namespace (or User): The account or organization name (e.g.,
myuser). Official Docker Hub images (likenginxorubuntu) 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.
3.4 Tags vs Digests
Section titled “3.4 Tags vs Digests”When pulling images for production, how you reference the image heavily impacts stability and security.
latest Tag
Section titled “latest Tag”By default, if you don’t specify a tag (e.g., docker pull nginx), Docker implicitly pulls nginx:latest.
- Warning:
latestis just a standard string. It does not automatically mean “the newest version”. It simply means the image was explicitly tagged with the wordlatestby the maintainer. Never use:latestin production, as updates can break your app unexpectedly.
Version Tags & Semantic Versioning
Section titled “Version Tags & Semantic Versioning”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)
SHA256 Digest
Section titled “SHA256 Digest”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.
Immutable Image References
Section titled “Immutable Image References”Pulling by digest ensures that you receive the exact same bits every single time, even if the tag is maliciously altered or deleted.
docker pull nginx@sha256:4c0fdaa8b6241a788c...