Keyboard Shortcuts N Next post
P Previous post
S Save / unsave
R Read aloud
T Toggle theme
/ Focus search
Esc Close panels
🔥
Ready to read...
Docker Docker & Containers: From Zero to Production Docker Images Module 2 — Docker Images

What Is a Docker Image? Layers Explained Visually

Reviewed & accurate
AI Summary

What You'll Learn

Beginner

  • What a Docker image is (and how it differs from a container)
  • How layers work and why they save disk space
  • The difference between read-only image layers and the writable container layer
Writable Container Layer (ephemeral — deleted when container stops) Layer 4: COPY . /app (your code) 12 MB Layer 3: RUN npm install (dependencies) 145 MB Layer 2: RUN apt-get install curl 8 MB Layer 1: FROM ubuntu:22.04 (base image) (read-only, shared across all containers using it) 77 MB ↑ R/W Read-Only ↓ Key insight: Each instruction in your Dockerfile creates a new read-only layer. When you run a container, Docker adds a writable layer on top.
Docker image layers — read-only stack with a writable top layer per container

Image vs Container

ImageContainer
Read-only templateRunning instance of an image
Stored on disk, doesn't changeCreated when you docker run
Can be shared, pushed, pulledHas a writable layer on top
Like a class in OOPLike an object (instance)

How Layers Work

Each instruction in a Dockerfile creates one layer:

Dockerfiledockerfile
FROM ubuntu:22.04          # Layer 1: base image (77 MB)
RUN apt-get install curl   # Layer 2: adds curl (8 MB)
COPY app /app              # Layer 3: adds your code (12 MB)
CMD ["./app/run"]          # No new layer — metadata only

When you run docker run myapp, Docker stacks these read-only layers and adds a thin writable layer on top. Any changes the container makes go to this writable layer — the image is never modified.

Why Layers Matter

Three big benefits
  • Sharing: Two containers from the same image share all layers — no duplication.
  • Caching: If a layer hasn't changed, Docker reuses it during builds — faster builds.
  • Efficiency: Only changed layers are pushed/pulled — not the whole image.

Common Docker Image Commands

Terminalbash
docker images              # List local images
docker pull nginx          # Download an image
docker rmi nginx           # Remove an image
docker image prune         # Remove unused images
docker image inspect nginx # View image details
docker tag nginx myrepo/nginx:v1  # Tag an image

Image Naming Convention

Image name formatbash
[registry/]repository[:tag]

# Examples:
nginx                    # Docker Hub, official, latest
ubuntu:22.04             # Docker Hub, official, version 22.04
myuser/myapp:v1.0        # Docker Hub, user repo, version v1.0
ghcr.io/org/repo:latest  # GitHub Container Registry
Always pin versions
Don't use latest in production. It's a moving target — your image changes without warning. Pin to a specific version: nginx:1.25.3 instead of nginx:latest.

Practical Exercise

Run docker pull nginx:alpine
Run docker images — see the image and its size
Run docker history nginx:alpine — see each layer and its size
Run docker run -d --name web nginx:alpine
Run docker exec web touch /tmp/test — writes to the container's writable layer
Run docker stop web && docker rm web — the writable layer is deleted

Key Takeaways

  • An image is a read-only template. A container is a running instance with a writable layer on top.
  • Each Dockerfile instruction creates one layer.
  • Layers are shared across containers from the same image — saves disk and download time.
  • Layer caching speeds up builds — unchanged layers are reused.
  • Always pin image versions (nginx:1.25.3), avoid latest> in production.
Previously: Lesson 08 covered container lifecycle.
Today: You learned what images are and how layers work.
Next: Lesson 10 covers pulling images from Docker Hub.
Test Your Knowledge
How did you find this?

Comments

Join the discussion! Sign in with your Google or Blogger account, or comment as Anonymous - no account needed. For quick questions, also reach me on Telegram @cytestch.

Comments