Writing a Dockerfile
A Dockerfile is a recipe for building your own image. Let's containerize a small Node.js web app.
The app
Create a folder hello-docker with two files:
// package.json
{
"name": "hello-docker",
"version": "1.0.0",
"main": "server.js",
"scripts": { "start": "node server.js" },
"dependencies": { "express": "^4.21.0" }
}// server.js
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;
app.get('/', (req, res) => res.send(`Hello from a container! 🐳 (${process.env.APP_ENV || 'dev'})`));
app.get('/health', (req, res) => res.json({ ok: true }));
app.listen(PORT, () => console.log(`Listening on ${PORT}`));The Dockerfile
# 1. Start from an official base image (pin the version!)
FROM node:22-alpine
# 2. Set the working directory inside the image
WORKDIR /app
# 3. Copy dependency files first (for better caching — see below)
COPY package*.json ./
# 4. Install dependencies
RUN npm ci --omit=dev
# 5. Copy the rest of the source code
COPY . .
# 6. Default environment variables
ENV NODE_ENV=production PORT=3000
# 7. Document the port the app listens on
EXPOSE 3000
# 8. The command that runs when a container starts
CMD ["node", "server.js"](npm ci needs a package-lock.json, so run npm install once locally to create it.)
Build and run
docker build -t hello-docker:1.0 .
docker run -d -p 3000:3000 -e APP_ENV=docker --name hello hello-docker:1.0
curl http://localhost:3000-t names and tags the image. The . is the build context: the folder whose files Docker can COPY.
The instructions
| Instruction | Purpose |
|---|---|
FROM |
Base image to build on |
WORKDIR |
Set (and create) the working directory |
COPY |
Copy files from the build context into the image |
RUN |
Execute a command at build time (creates a layer) |
ENV |
Set environment variables |
ARG |
Build-time variable (docker build --build-arg) |
EXPOSE |
Document which port the app uses |
USER |
Which user to run as (lesson 9) |
CMD |
Default command at run time (can be overridden) |
ENTRYPOINT |
Fixed executable; CMD becomes its default arguments |
RUNhappens while building the image.CMDhappens when a container starts. Mixing these up is the most common beginner bug.
Layer caching: order matters
Docker caches each layer. If a step's inputs haven't changed, it reuses the cached layer, but once one layer changes, every layer after it is rebuilt.
That's why we copy package*.json and run npm ci before copying the source code: editing server.js then only rebuilds the last COPY, not the slow dependency install.
# ✗ Slow: any code change reinstalls all dependencies
COPY . .
RUN npm ci
# ✓ Fast: dependencies are cached until package.json changes
COPY package*.json ./
RUN npm ci
COPY . ..dockerignore
Keep junk and secrets out of the build context, the same way .gitignore does for Git:
# .dockerignore
node_modules
npm-debug.log
.git
.env
Dockerfile
*.mdWithout this, COPY . . would copy your local node_modules (wrong OS binaries!) and your .env secrets into the image.
The same idea in Python
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["gunicorn", "-b", "0.0.0.0:8000", "app:app"]Inside a container, servers must listen on
0.0.0.0, notlocalhost/127.0.0.1. Otherwise they're unreachable from outside the container.
Practice
- Containerize the Node app above and run it.
- Change the message in
server.js, rebuild, and watch which steps sayCACHED. - Override the command at run time:
docker run --rm hello-docker:1.0 node -e "console.log('override!')".
1.Why copy package.json and install dependencies before copying source code?
2.What's the difference between RUN and CMD?