Running an Express server in a Docker container

See my previous post on Getting started with nodejs and express for the Express app itself. This post packages that app into a Docker image.
A Docker container bundles an application with its runtime, libraries and dependencies, and runs it as an isolated process on the host. The practical benefit for a Node app is that the Node version and installed packages are fixed in the image, so the server behaves the same on your laptop, in CI and in production. Containers share the host’s kernel instead of booting a full guest OS like a virtual machine, so they start quickly and use less memory.
The Dockerfile
Section titled “The Dockerfile”Put this Dockerfile in the project root, next to package.json:
# Node.js 26 is an LTS release; the alpine variant keeps the image smallFROM node:26-alpine
# Set the working directory (created if it doesn't exist)WORKDIR /usr/src/app
# Copy only the dependency manifests first so this layer is cachedCOPY package*.json ./
# Install exactly what's in package-lock.json, without dev dependenciesRUN npm ci --omit=dev
# Copy the application source codeCOPY . .
# Document the port the server listens onEXPOSE 3000
# Start the Express serverCMD ["npm", "start"]Copying package*.json and installing before copying the rest of the source means Docker can reuse the npm ci layer when you only change application code, which makes rebuilds much faster. npm ci requires a package-lock.json; if you don’t have one, run npm install locally first to create it.
EXPOSE doesn’t publish anything by itself. It documents the port; you still map it with -p when you run the container.
Add a .dockerignore so your local node_modules (which may contain binaries built for a different OS) isn’t copied over the one installed in the image:
node_modulesnpm-debug.log.gitThe project should look like this:
my-express-app/├── .dockerignore├── Dockerfile├── app.js├── package.json└── package-lock.jsonpackage.json needs a start script, for example "start": "node app.js", and the app should listen on port 3000. Call app.listen(3000) without a host argument (or with 0.0.0.0). If the server binds to 127.0.0.1, it is only reachable from inside the container.
Build and run
Section titled “Build and run”Build the image from the directory containing the Dockerfile:
docker build -t my-express-app .Then run it, mapping port 3000 on your machine to port 3000 in the container:
docker run -p 3000:3000 my-express-appThe server is now at http://localhost:3000. Add -d to run it in the background, and stop it with docker stop <container-id> (docker ps lists running containers). Ctrl+C may not stop it: the process running as PID 1 in a container ignores SIGINT unless it installs a handler, so either handle the signal in your app or add --init to docker run.
References
Section titled “References”This content came from a book I generated using chatGPT and a lot of editing.
View on the source material see amazon https://www.amazon.com/dp/B0BSL14M71
For a free copy, please view https://friendlyuser.github.io/assets/pdfs/introToJs.pdf
© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)