Images & the Dockerfile — Module Overview
What is a Docker image?
Section titled “What is a Docker image?”A Docker image is a read-only package that contains everything needed to run a program — the OS filesystem, runtime, libraries, application code, and default configuration. Think of it as a recipe: the recipe itself never changes, but you can bake as many cakes (containers) from it as you like.
Images are built from stacked, read-only layers. Each layer represents a change — a file added, a package installed, a configuration written. When you stack layers on top of each other you get a complete filesystem, but Docker only stores (and transfers) the layers that have changed, keeping images lean.
flowchart TB img["Image (read-only)"] l3["Layer 3: COPY app/ /app/ (your code)"] l2["Layer 2: RUN npm install (dependencies)"] l1["Layer 1: FROM node:22-alpine (base OS + runtime)"] img --> l3 --> l2 --> l1
A running container is an image with one extra writable layer on top. The image is untouched — the container writes to its own scratch layer. Stop the container and that layer disappears (unless you save it).
flowchart TB con["Container (running)"] w["Writable layer (container-specific changes, deleted on stop)"] l3["Layer 3 (read-only)"] l2["Layer 2 (read-only)"] l1["Layer 1 (read-only)"] con --> w --> l3 --> l2 --> l1
What is a Dockerfile?
Section titled “What is a Dockerfile?”A Dockerfile is a plain-text file of instructions that tells Docker how to build an image. Each instruction becomes a layer. You run docker build to turn a Dockerfile into an image, and docker run to start a container from that image.
Here is the simplest possible Dockerfile:
# syntax=docker/dockerfile:1FROM alpine:3.20CMD ["echo", "Hello, Docker!"]Build and run it:
docker build -t hello .docker run --rm helloExpected output:
Hello, Docker!What this module covers
Section titled “What this module covers”| Lesson | What you will learn |
|---|---|
| This page | Images vs containers, layers, first build |
| Dockerfile basics | Core instructions: FROM, WORKDIR, COPY, RUN, ENV, EXPOSE |
| Build & tag | Build context, -t name:tag, docker images, .dockerignore |
| Layers & cache | How build cache works, instruction ordering for speed |
| CMD vs ENTRYPOINT | Exec form, shell form, default arguments, overrides |
Hands-on: your first build
Section titled “Hands-on: your first build”Paste the snippet below into a Play with Docker session, run the two commands, and see Docker build a tiny image from scratch.
# syntax=docker/dockerfile:1
FROM alpine:3.20
CMD ["echo", "Hello, Docker!"]
# --- build & run ---
# docker build -t hello .
# docker run --rm hello