Latest
Docker 1 min read

Run a Node.js Web Server in Docker

Run a Node.js Web Server in Docker
Docker · November 24, 2026

Docker lab/version note: use supported Linux-container tools and patched images. Check version compatibility before running the examples. Docker, Swarm and platform-specific deployment steps were not tested live.

What this guide covers

A Docker image packages an application and its runtime. This guide creates a small Node.js web server, builds an image and runs it as a non-root user with a local port mapping.

Before you start

Install Docker with Linux-container support. Use a new directory for these files and run all build commands there. The sample uses only Node’s built-in modules, so it needs no npm dependencies.

Step 1: Create the web server

Save this as server.mjs:

import http from 'node:http';
const port = Number(process.env.PORT || 3000);
const server = http.createServer((req, res) => {
  res.setHeader('Content-Type', 'application/json');
  res.end(JSON.stringify({
    status: 'ok',
    version: process.env.APP_VERSION || 'dev',
    path: req.url,
  }));
});
server.listen(port, '0.0.0.0');
process.on('SIGTERM', () => {
  server.close(() => process.exit(0));
  setTimeout(() => process.exit(1), 8000).unref();
});

The server listens on all container interfaces so Docker can forward traffic to it. It returns JSON for every path, including /healthz; that checks this process only, not a database or other dependency.

Step 2: Create the image files

Save this as .dockerignore to avoid sending local dependencies and environment files to the builder:

.git
node_modules
coverage
dist
.env
.env.*
*.log

Save this as Dockerfile:

FROM node:24-bookworm-slim
ENV NODE_ENV=production PORT=3000
WORKDIR /app
COPY --chown=node:node server.mjs ./server.mjs
USER node
EXPOSE 3000
CMD ["node", "server.mjs"]

WORKDIR sets the application directory. COPY adds the source. USER node runs the application without root. EXPOSE documents the port; it does not publish it on the host.

Step 3: Build and run

Run:

docker version
docker build --pull -t node-lab:1.0 .
docker image inspect node-lab:1.0 --format '{{.Config.User}} {{json .Config.Cmd}}'
docker run -d --name node-lab \
  --read-only --tmpfs /tmp:rw,noexec,nosuid,size=16m \
  --cap-drop ALL --security-opt no-new-privileges=true \
  --memory 128m --cpus 0.5 \
  -e APP_VERSION=1.0 -p 127.0.0.1:8080:3000 node-lab:1.0
curl --fail http://127.0.0.1:8080/healthz
docker logs --tail 50 node-lab
docker stats --no-stream node-lab

The published host address is local only. curl should return JSON. If it cannot connect, read docker logs node-lab and check the port mapping.

Step 4: Stop and remove the test container

Run docker stop node-lab, then docker rm node-lab. The image remains available for another container. This example stores no application data; use a volume or external database for data that must survive replacement.

Check the result

Confirm the HTTP response, configured image user and clean shutdown. Review a current patched base image and record its digest before a production deployment.

References

PB
Written by

Parul Bedi

Notes and guides by Parul Bedi.

Up next

Install SonarQube on Fedora Step by Step

Sonar November 26, 2026 5 min read
Open post

Conversation

0 comments

No comments yet. Start the conversation below.

Leave a comment

Replying to a comment.
Be kind. Markdown is not supported in comments.