The Dockerfile, Line by Line
The complete production Dockerfile of this site, every instruction explained, including the two that most tutorials get wrong: layer order and the user.
The whole file
FROM node:22-slim
RUN apt-get update && apt-get install -y --no-install-recommends fonts-dejavu-core fontconfig && rm -rf /var/lib/apt/lists/*
WORKDIR /app
RUN chown node:node /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY --chown=node:node . .
USER node
EXPOSE 8080
CMD ["node", "server.js"]
Twelve lines. Here is what each does and why it is written this way.
FROM node:22-slim
Every image starts from a base image. This one is the official Node.js 22 image in its slim variant: Debian with only what Node needs. The full image is several times bigger and contains compilers and tools a web server never uses. Every extra package is extra download time and extra security surface.
Pin the major version (22) so a rebuild next month does not silently jump to a new Node. Match it to the version CI tests on.
Installing fonts, in one RUN
RUN apt-get update && apt-get install -y --no-install-recommends fonts-dejavu-core fontconfig && rm -rf /var/lib/apt/lists/*
The site draws some images on the server (share cards, for example), and text in an image needs fonts. Three habits in this one line:
- One
RUN, joined with&&. EachRUNis a layer. Splitting update, install and clean-up into three lines would store the package lists in a layer forever, even after they are "deleted" in a later one. --no-install-recommendsinstalls only what was asked for, not every suggested extra.rm -rf /var/lib/apt/lists/*removes the downloaded package index in the same layer, so it never reaches the image.
WORKDIR and ownership
WORKDIR /app creates the folder and makes it the working directory for everything that follows. chown node:node /app hands it to the unprivileged node user that the official image already provides. That user will run the app later.
Dependencies before code: the cache trick
COPY package*.json ./
RUN npm ci --omit=dev
Only the two package files are copied first, then dependencies are installed. Why not copy everything at once? Because of layer caching. Dependencies change rarely; code and content change on every commit. With this order, a commit that only edits a Markdown file reuses the cached dependency layer and skips the slow install entirely. Copy everything first, and every commit reinstalls every package.
npm ci --omit=dev installs exactly what the lock file says, production packages only.
Copying the application
COPY --chown=node:node . .
Now the rest of the repository is copied in, owned by the node user. What is not copied is decided by .dockerignore: .git, .env, node_modules, backups and documentation stay out. A missing line in .dockerignore is how secrets end up inside images.
USER node: never run as root
By default, a container process runs as root. If an attacker ever finds a way to run code through the app, root inside the container makes every next step easier. USER node drops to an ordinary user for everything after this line, including the server itself. A test on this site fails if this line ever disappears.
EXPOSE and the port
EXPOSE 8080 documents the port the app listens on. It does not open anything by itself. Cloud Run tells the container which port to use through the PORT environment variable, and the server reads process.env.PORT with 8080 as a fallback. The same image therefore runs unchanged on a laptop and on Cloud Run.
CMD in exec form
CMD ["node", "server.js"] starts the server. The square-bracket exec form runs Node directly as the main process, so it receives stop signals from the platform and can shut down cleanly. The plain string form (CMD node server.js) wraps it in a shell that may not pass those signals on.
Try it yourself
Take any Dockerfile you have and check four things: is the base image slim and pinned to a major version, are dependencies installed before the code is copied, is there a USER line, and is CMD in exec form? Each "no" is a one-line fix.