Skip to content

Docker Images

Build, tag, and push Docker images using layers, registries, and best practices.

What you'll learn

  • How Docker images are built from layers
  • Building images with docker build
  • Tagging and pushing images to registries
  • Optimizing images with multi-stage builds

Concept

Image Layers

A Docker image is built up from a series of layers. Each instruction in a Dockerfile — FROM, RUN, COPY, ADD — creates a new layer. Layers are cached, so if a layer hasn't changed, Docker reuses it on the next build. This makes rebuilds fast. Layers are also shared between images, saving disk space. Understanding layers is key to writing efficient Dockerfiles: you want to put rarely-changing instructions first and frequently-changing ones last to maximize cache hits.

Building Images

The docker build command reads a Dockerfile and produces an image. You specify a context (the directory Docker can access during the build) and optionally a tag. For example, docker build -t myapp:1.0 . builds the Dockerfile in the current directory and tags the result as myapp:1.0. The .dockerignore file excludes files from the context, similar to .gitignore, which speeds up builds and prevents secrets from being copied into the image.

Tagging and Registries

A tag is a label for an image, typically name:version like myapp:1.0 or myapp:latest. A registry is a storage service for images — Docker Hub is the public default, but teams use private registries like Amazon ECR or GitHub Container Registry. You push images with docker push and pull them with docker pull. Tagging images with semantic versions instead of just latest makes deployments reproducible and rollbacks easy.

Multi-Stage Builds

Multi-stage builds use multiple FROM instructions in a single Dockerfile. You build your application in a first stage with all build tools, then copy only the compiled output into a smaller final image. This dramatically reduces the final image size — often from hundreds of megabytes to tens — by excluding compilers, source code, and intermediate files from the production image.

Image Security

Scan images for vulnerabilities with tools like Trivy or Docker Scout. Use distroless base images that contain only your application and its runtime — no shell, no package manager, no extra binaries. This dramatically reduces the attack surface. Sign images with Cosign to verify their integrity in production. Pull images by digest (@sha256:...) instead of tag for maximum reproducibility — a tag can be reassigned, but a digest is immutable and always points to the exact same image layers.

Example

              # Build an image from a Dockerfile
docker build -t myapp:1.0 .
docker build -t myapp:1.0 -f Dockerfile.prod .   # custom Dockerfile

# Tag and push to a registry
docker tag myapp:1.0 registry.example.com/myapp:1.0
docker push registry.example.com/myapp:1.0

# Inspect image layers and size
docker image history myapp:1.0      # show layer history
docker images myapp                 # list image variants

# Multi-stage build (in Dockerfile)
# FROM node:20 AS builder
# COPY . . && RUN npm run build
# FROM nginx:alpine
# COPY --from=builder /dist /usr/share/nginx/html
            

docker build creates images, docker tag and docker push publish them to registries, and multi-stage builds reduce final image size by excluding build tools.

Try it

Common mistakes

The mistake

Using the :latest tag in production

The fix

Tag images with specific versions like myapp:1.2.3. The :latest tag is mutable and can point to different images over time, making deployments non-reproducible.

The mistake

Putting COPY . . before RUN npm install

The fix

Order Dockerfile instructions from least to most frequently changing. Copy package.json first, install dependencies, then copy source code. This maximizes layer cache hits.

Practice Docker Images

4 exercises

Practice Docker Images

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors