Skip to the page

Containers, Dockerfiles and layers · core

Writing a Dockerfile

A Dockerfile is a list of build instructions: FROM picks a base image, WORKDIR, COPY and RUN prepare the filesystem, ENV and USER set the runtime environment, and CMD or ENTRYPOINT say which process starts, written in exec (JSON array) form so signals reach it.

The notes

What you must know

  • FROM node:24-slim: pin a major (or exact) version; avoid 'latest'. Smaller bases (slim, alpine, distroless or Docker Hardened Images) mean fewer packages to patch.
  • WORKDIR /app sets the folder; COPY copies files from the build context (prefer COPY over ADD, which also unpacks archives and fetches URLs); RUN runs commands at build time.
  • ENV sets variables in the image (NODE_ENV=production). ARG is build-time only, but its values can still appear in the image history, so never pass secrets through ARG or ENV.
  • CMD vs ENTRYPOINT: ENTRYPOINT is the fixed executable, CMD the default arguments (or the whole command if there is no ENTRYPOINT). Use the exec form CMD ["node", "server.js"]; the shell form wraps it in /bin/sh, which may not pass SIGTERM on.
  • EXPOSE only documents a port; publishing happens with -p 8080:3000 or in compose.
  • USER node (the non-root user in official Node images) for the running process.
  • .dockerignore keeps node_modules, .git, .env, logs and build output out of the build context: faster builds and no leaked secrets.
  • docker init scaffolds a Dockerfile, compose.yaml, .dockerignore and README.Docker.md for a Node project.

The red pen

Common traps: mistake → fix

  • Mistake: EXPOSE 3000 makes the app reachable

    Fix: you still publish the port with -p or compose 'ports'.

  • Mistake: ARG is safe for an npm token because it is not in the final ENV

    Fix: build args can show up in the image history; use a BuildKit secret mount.

  • Mistake: CMD npm start is the same as CMD ["node", "server.js"]

    Fix: npm adds a process that can swallow SIGTERM, so shutdowns are not graceful.

Easy to hard

Questions interviewers ask, easy to hard

  1. Walk me through a Dockerfile for an Express API.
  2. CMD vs ENTRYPOINT? Shell form vs exec form?
  3. ARG vs ENV?
  4. What goes in .dockerignore and why?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview