# Docker Tutorial for Beginners: Image, Container, Dockerfile

> Learn Docker from a real Dockerfile: what an image and a container are, how layers and the build cache work, and how to order instructions for fast builds.

URL: https://www.matthewswong.com/en/learn/devops-from-zero/docker-tutorial-for-beginners/

DevOps from zero · Lesson 3 of 25 · Reviewed on 13 October 2026 · By Matthews Wong (https://www.matthewswong.com/en/about/)

> A Docker image is a read-only stack of layers, built from the instructions in a Dockerfile; a container is a running copy of that image with one thin writable layer on top. Docker caches each layer, so ordering instructions from least to most often changed is what keeps rebuilds fast.

## You will be able to

- Explain the difference between an image, a layer and a container.
- Predict which steps of a Docker build come from cache after you edit a file.
- Order a Dockerfile and a .dockerignore so builds are fast and secrets stay out.

## How a Docker image is built, layer by layer

The build context goes to docker build, each Dockerfile instruction adds a layer to the image, an edit rebuilds only the layers after it, and running the image adds a writable layer.

1. **Meet the parts.** A Dockerfile is a list of instructions. Most of them add one read-only layer to the image, and the layers stack from the bottom up.
2. **Send the build context.** docker build first sends the build context. .dockerignore keeps node_modules, .git and .env out, so they can never end up in the image.
3. **Start from a base.** FROM starts from an existing image. Its layers are reused exactly as they are; nothing in them is copied or changed.

   ```
   FROM node:20-alpine AS deps
   ```
4. **Dependencies first.** Copy only the dependency files, then install. This layer changes only when package-lock.json changes, and a cache mount keeps downloaded packages between builds.

   ```
   COPY package.json package-lock.json ./
   RUN --mount=type=cache,target=/root/.npm npm ci
   ```
5. **Then the source and build.** Now copy the source code and build it. Source changes often, so these instructions come last in the file.

   ```
   COPY . .
   RUN npm run build
   ```

   *Predict:* You edit one source file and build again. What runs again? (Every instruction, from FROM down / COPY . . and everything after it / Only RUN npm ci)
6. **Edit one file, rebuild.** Edit one file and build again. The base and install layers come from cache; COPY . . and every layer after it run again.

   *Predict:* What does running the image add to it? (A thin writable layer on top / A writable copy of every layer)
7. **Run it.** Running the image adds one thin writable layer on top. The image layers stay untouched, so many containers can share them.

## What is the difference between a Docker image and a container?

An image is a read-only template: a stack of layers, where each layer is a set of filesystem changes left by one Dockerfile instruction. Once a layer is created it never changes. A container is what you get when you run an image: Docker puts one thin writable layer on top, and everything the running process writes lands there.

That split is why ten containers from one image cost little extra disk: they share the same read-only layers and each keeps only its own writable layer. It is also why anything a container writes disappears when the container is removed, unless you mount a volume.

## Why does the order of a Dockerfile decide how fast it rebuilds?

Docker caches each layer. On the next build it reuses a layer when that instruction and its inputs have not changed. But invalidation only flows downstream: when one layer changes, every layer after it is rebuilt too, even if those instructions are identical.

So the rule is to order instructions from the least to the most often changed. Dependencies change rarely and take the longest to install, so copy only package.json and package-lock.json and run the install before you copy the rest of the source. A one-line code change then reuses the install layer and rebuilds in seconds.

## What does .dockerignore protect you from?

Before building, docker build sends the build context: the set of files the build can access, normally the project folder. Anything in it can be copied into the image by a careless COPY . . instruction. A .dockerignore file removes paths from the context, so node_modules, .git and .env files never reach the builder at all.

The same file has a sharp edge. A pattern like *.md matches Markdown files in the context root only; files in subfolders are still included. Exceptions start with !, and the last matching line wins, so an exception placed before the rule it overrides does nothing.

## Why npm ci, and what does a cache mount add?

npm ci installs exactly what package-lock.json says, so every build gets the same dependency tree. npm install may update the lockfile and let builds drift. In a Dockerfile you want the reproducible one.

A cache mount, written RUN --mount=type=cache, gives that one instruction a persistent folder that lives outside the image layers. Point it at npm's download cache and a lockfile change still re-runs npm ci, but npm re-uses the packages it already downloaded instead of fetching every tarball again.

## The real config behind this lesson

`Dockerfile:1-11`

```dockerfile
# syntax=docker/dockerfile:1
# The syntax directive is what enables `--mount=type=cache` below.

FROM node:20-alpine AS deps
WORKDIR /app
COPY package.json package-lock.json ./
# Cache npm's download directory, not node_modules. This survives within a build
# even when the layer above is invalidated, so a lockfile change re-links from
# cache instead of re-downloading every tarball. Still `npm ci`, so the lockfile
# stays authoritative.
RUN --mount=type=cache,target=/root/.npm npm ci
```

The first stage of this site's image. Only the two dependency files are copied before the install, so editing a page never re-runs npm ci. The cache mount points at npm's download folder, not node_modules: a lockfile change still installs cleanly, it just stops re-downloading everything. Line 1 is what turns on the --mount syntax.

`.dockerignore:4-10`

```text
node_modules
.git
.env
.env.*
.DS_Store
npm-debug.log*
*.log
```

The first lines of this site's .dockerignore. Local dependencies, the git history and every .env file are kept out of the build context, so a COPY . . later in the Dockerfile can never copy a secret into the image.

`.dockerignore:37-41`

```text
# NOT excluded, deliberately — each of these looks like dead weight and is not:
#   public/    78 MB, but the runner stage copies it into the final image
#   messages/  12 MB, compiled into the server bundle by next-intl
#   scripts/   10 MB, because `prebuild` runs generate-blog-sources.mjs --check,
#              check-placements.mjs and check-imports.mjs
```

The other half of the rule. These folders look heavy, and excluding them would make the build fail or the site break. Before you ignore something large, check whether the build reads it.

## What breaks

- **COPY . . before the install.** Copying the whole project before npm ci ties the install layer to every source file, so a one-line change reinstalls all dependencies. Copy the manifests, install, then copy the rest.
- **A secret baked into a layer.** Without a .dockerignore, an .env file rides in with COPY . . and stays in that layer for good. Deleting it in a later instruction does not help: the earlier layer still holds it. Keep secrets out of the context and pass them at runtime.
- **Expecting a cache mount to survive in CI.** On a fresh CI runner the cache mount starts empty, and the GitHub Actions cache does not save it by default. This site works around that for Next's compile cache with a .dockerignore exception, !.next/cache, so the restored cache rides in with the build context.
- **What I'd change here: the base image.** This site still builds on node:20-alpine, and Node 20 reached end of life on 30 April 2026. A base image stops getting security fixes when its runtime does, so the next real edit to this Dockerfile is moving to a supported Node line.

### Image or container: what each one is

|  | Image | Container |
| --- | --- | --- |
| What it is | A read-only stack of layers | A running copy of an image |
| Can it change? | No: you build a new image instead | Yes, in its writable layer |
| Created by | docker build | docker run |
| When removed | Layers go once no image uses them | Its writable layer is gone |

## Try it yourself

You need Docker and Node installed, nothing else. Make a folder with a tiny server, build it twice, and watch which steps come from cache.

1. Create package.json and src/server.js, then run npm install once to create package-lock.json.

   ```bash
   { "name": "devops-lab", "version": "1.0.0", "private": true }
   
   // src/server.js
   require("node:http")
     .createServer((req, res) => res.end("hello\n"))
     .listen(3000, "0.0.0.0");
   ```
2. Write a Dockerfile that installs dependencies before copying the source, and a .dockerignore that keeps node_modules out.

   ```bash
   # syntax=docker/dockerfile:1
   FROM node:22-alpine
   WORKDIR /app
   COPY package.json package-lock.json ./
   RUN --mount=type=cache,target=/root/.npm npm ci
   COPY . .
   CMD ["node", "src/server.js"]
   
   # .dockerignore
   node_modules
   .git
   ```
3. Build it, edit the server, and build again. The npm ci step shows CACHED; COPY . . runs again.

   ```bash
   docker build -t devops-lab:v1 .
   echo "// tweak" >> src/server.js
   docker build -t devops-lab:v1 .
   ```
4. List the layers with their sizes, then run a container and open http://localhost:8080.

   ```bash
   docker history devops-lab:v1
   docker run --rm -p 8080:3000 devops-lab:v1
   ```

## Quick quiz

1. **A Dockerfile runs COPY . . and then RUN npm ci. You fix a typo in one component and rebuild. What happens to the install step?**
   A. It runs again and reinstalls every dependency
   B. It comes from cache, since package.json did not change
   C. Only the changed file is installed again
   Answer: A. The install layer sits after COPY . ., so any source change invalidates it and every layer after it. Copy package.json and package-lock.json, install, then copy the rest.

2. **There is no .dockerignore, and an .env file slipped into the project. The Dockerfile runs COPY . . and then RUN rm .env. Is the secret out of the image?**
   A. Yes, rm deletes it from the final image
   B. Yes, Docker never copies dotfiles
   C. No, the COPY layer still holds it
   Answer: C. A layer never changes once it is created, so deleting the file in a later instruction does not touch the layer that copied it. Keep secrets out of the build context with .dockerignore and pass them at runtime.

3. **Locally, a lockfile change still builds fast because the cache mount keeps npm's downloads. On a fresh GitHub Actions runner, every build downloads every package again. Why?**
   A. npm ci ignores cache mounts inside CI
   B. The cache mount starts empty on a fresh runner
   C. .dockerignore drops /root/.npm from the context
   Answer: B. A cache mount lives outside the image layers and starts empty on a fresh CI runner, and the GitHub Actions cache does not save it by default. This site works around that for Next's compile cache with a .dockerignore exception, !.next/cache.

## Think it through

- **You change one line in a component. Which Dockerfile instructions run again?** COPY . . and everything after it. Invalidation only flows downstream, and the install layer depends only on package.json and package-lock.json.
- **Why copy package.json and package-lock.json before COPY . .?** So the slow install layer depends only on the files that change least. Docker's own guidance is to put the steps that change often near the end of the Dockerfile.
- **This site's .dockerignore contains *.md. Does that exclude docs/guide.md?** No. *.md matches Markdown files in the context root only; files in subfolders are still included. The site excludes the docs folder on its own line.

## Go deeper

- [Docker Best Practices for Production Containers](https://www.matthewswong.com/en/blog/docker-containerization-best-practices/)
- [Docker BuildKit Cache Mounts: Cut CI Build Times in Half](https://www.matthewswong.com/en/blog/docker-buildkit-cache-mounts-faster-builds/)
- [The Docker Workflow I Use on Every Project](https://www.matthewswong.com/en/blog/docker-workflow-every-project/)
- [Learn Docker from Scratch: First Container in 6 Steps](https://www.matthewswong.com/en/blog/belajar-docker-dari-nol-panduan-pemula/)

## Sources

- [Docker docs: Understanding image layers](https://docs.docker.com/get-started/docker-concepts/building-images/understanding-image-layers/)
- [Docker docs: Docker build cache](https://docs.docker.com/build/cache/)
- [Docker docs: Optimize cache usage in builds](https://docs.docker.com/build/cache/optimize/)
- [Docker docs: Build context and .dockerignore](https://docs.docker.com/build/concepts/context/)
- [Docker docs: Cache management with GitHub Actions](https://docs.docker.com/build/ci/github-actions/cache/)
- [Node.js release schedule](https://github.com/nodejs/Release)

---

- HTML version: https://www.matthewswong.com/en/learn/devops-from-zero/docker-tutorial-for-beginners/
- Bahasa Indonesia: https://www.matthewswong.com/id/learn/devops-from-zero/docker-tutorial-for-beginners/
- Site index for agents: https://www.matthewswong.com/llms.txt
- Sitemap: https://www.matthewswong.com/sitemap.xml
- API description (OpenAPI): https://www.matthewswong.com/openapi.json
