Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

A Complete Dockerfile for a Node.js App

Save this as Dockerfile, with no extension, in the project folder next to package.json:

FROM node:26-slim
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci --omit=dev
COPY . .
USER node
EXPOSE 3000
CMD ["node", "server.js"]

Build the image and start a container from it:

docker build -t myapp:1.0 .
docker run -d --name myapp -p 127.0.0.1:3000:3000 myapp:1.0

The dot at the end of docker build is the build context: the folder whose files COPY can see. -t names the image, in lowercase only (see Docker invalid reference format). The container listens on the loopback address only, so a web server on the same host can pass requests to it, as in Nginx reverse proxy.

What Each Line Does

InstructionPurpose
FROMBase image to start from
WORKDIRCreates and enters a folder
COPYCopies files from the context
RUNRuns a command during the build
USERUser for later steps and the container
EXPOSEDocuments a port; publishes nothing
CMDDefault command at start

The official Node.js images include an unprivileged user named node with UID 1000, so USER node keeps the application from running as root. Files copied before that line belong to root: the application can read them but not change them. If it must write to its own folder, use COPY --chown=node:node . ., or better, a volume. A port listed in EXPOSE is reachable only after you publish it with -p; see Docker port mapping.

Layer Caching: Why the Package Files Come First

Each instruction produces a layer, and Docker reuses a cached layer as long as the instruction and its input have not changed. Once one step changes, it and every step after it run again. Copying only the package files and running npm ci before COPY . . means a change in your code reuses the cached dependency layer. npm ci installs the exact versions from package-lock.json and stops with an error if that file is missing or out of date; --omit=dev leaves out development dependencies.

Keep Files Out with .dockerignore

node_modules
.git
.env
*.log

Save this as .dockerignore next to the Dockerfile. Without it, COPY . . copies your local node_modules over the installed one and puts the .env file, with its passwords, into the image. The patterns work much like those in .gitignore.

CMD, ENTRYPOINT, and the Exec Form

Write CMD as a JSON array, the exec form. The program then runs directly as the main process and receives the stop signal from docker stop. In the shell form, CMD node server.js, the program runs under /bin/sh -c, which does not pass signals on, so Docker waits out the timeout and kills it. Node.js itself was not designed to run as process 1, so either handle SIGTERM in your code or start the container with docker run --init, as the Node.js image's best practices advise.

ENTRYPOINT ["node"]
CMD ["server.js"]

ENTRYPOINT fixes the program; CMD gives default arguments that anything after the image name in docker run replaces. With this pair, docker run myapp:1.0 worker.js runs node worker.js. With CMD alone, the whole command is replaced.

Multi-Stage Builds

When the application needs a build step, such as compiling TypeScript, use two stages in one Dockerfile. The first, FROM node:26 AS build, installs all dependencies and runs the build. The second starts again from node:26-slim, installs production dependencies only, and takes the result with COPY --from=build /app/dist ./dist. Build tools stay in the first stage, so the final image is smaller.

Never Bake Secrets into an Image

Every file you copy and every ENV value stays in the image, and build arguments set with ARG are visible in docker history. Deleting a file in a later step does not remove it from the earlier layer. Pass passwords and keys when the container starts, with --env-file or a mounted file. For a secret needed during the build, BuildKit offers secret mounts (RUN --mount=type=secret) that are not stored in the image.

Pin the Base Image

FROM node:26-slim follows one major version; node:latest can move to a new one without warning. Rebuild regularly so the base image brings security fixes: docker build --pull -t myapp:1.1 . fetches the newest image under the same tag before it builds.

Was this answer helpful?
Loading...