Build images
Write the first Dockerfile
Describe the Notes API image with a base image, working directory, copied files, documented port, and startup command.
8 minute lesson
A Dockerfile is an ordered build recipe. Each instruction creates image metadata or a filesystem layer that later instructions can use.
FROM chooses the starting filesystem, WORKDIR changes the directory for later steps, COPY moves files from the build context, and CMD supplies the default process. EXPOSE documents a port but does not publish it.
FROM node:22-alpine
WORKDIR /app
COPY package.json server.js ./
ENV PORT=3000
EXPOSE 3000
CMD ["node", "server.js"]
Prefer the JSON form of CMD. Docker starts Node directly, so termination signals reach the application instead of first passing through an extra shell. This helps the process shut down cleanly during a deploy.
The Dockerfile separates build-time facts from runtime choices. COPY and RUN shape the immutable image; environment variables and port mappings can vary per container. EXPOSE 3000 is documentation for humans and tools, not a firewall rule and not the equivalent of -p. Also choose base-image versions deliberately: a tag is convenient, while a recorded digest gives a reproducible release input.
Save the file as Dockerfile, then build it with docker build -t notes-api:dev ..
Lesson completed