Sub topic 2
Writing Your First Dockerfile
This chapter assumes you can run a Node.js Express server locally, use a command line, and have Docker installed. It covers how a Dockerfile turns your code into an image. Building the image, running it, and tuning it come in later chapters.
What a Dockerfile is
A Dockerfile is a plain text file named Dockerfile that sits in your project root. Each line is an instruction. Docker reads the instructions top to bottom and produces an image. Every instruction that changes the filesystem adds one layer to that image.
A complete example
This Dockerfile packages a Node.js app that listens on port 3000 and starts from server.js.
FROM node:22-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
ENV NODE_ENV=production
EXPOSE 3000
USER node
CMD ["node", "server.js"]The instructions, line by line
FROM node:22-alpinepicks the base image. Every Dockerfile starts here. This base already contains Node.js 22 on Alpine Linux, a small distribution, so you do not install Node yourself.WORKDIR /appsets the working directory. Docker creates it if it does not exist. Later instructions run relative to it.COPY package*.json ./copiespackage.jsonandpackage-lock.jsonfrom your project into/app.RUN npm ci --omit=devruns a command at build time.npm ciinstalls exactly the versions in the lock file.--omit=devskips development dependencies.COPY . .copies the rest of your source code into/app.ENV NODE_ENV=productionsets an environment variable that is present in the image and in every container started from it.EXPOSE 3000documents the port the app listens on. It does not publish the port. You publish ports when you run the container.USER nodemakes later instructions and the running process use the unprivilegednodeuser instead of root.CMD ["node", "server.js"]sets the default command the container runs when it starts.
Why the copy steps are split
The file copies package*.json first, installs dependencies, and only then copies the rest of the source. Docker can reuse a layer when its inputs have not changed. If you edit server.js, the dependency layer stays the same and the slow install is skipped. A later chapter on build caching explains this in detail. For now, keep this order.
How the build flows
Common errors
- Shell form versus exec form.
CMD ["node", "server.js"]is exec form. It runs Node directly as the main process, sodocker stopsignals reach it. WritingCMD node server.jswraps it in a shell, which can delay clean shutdown. - Missing lock file.
npm cifails if there is nopackage-lock.json. Commit the lock file, or usenpm installif you have none. - Wrong file name. The default file name is
Dockerfile, with a capital D and no extension. Other names need the-fflag. - Forgetting that
EXPOSEis documentation. The app is not reachable from your machine until you publish the port at run time. - Copying everything.
COPY . .copies whatever is in the build context, includingnode_modulesand.env. The next chapter shows how to prevent that.
Exercise
In the Dockerfile above, why does COPY package*.json ./ appear before COPY . ., and which instruction runs when the container starts rather than when the image is built?
Show answer
Copying the dependency manifests first lets Docker cache the npm ci layer, so editing source files does not trigger a reinstall. CMD runs when the container starts. RUN runs at build time.
Exercise
A teammate says EXPOSE 3000 makes the app reachable at http://localhost:3000. Is that correct?
Show answer
No. EXPOSE only documents the port. To reach the app from the host, you must publish the port when running the container, for example with -p 3000:3000.