On this page
- What should be ready before you begin?
- How do you keep junk out of the build context?
- What does a good multi-stage Dockerfile look like?
- Why run as a non-root user, and how?
- How do you build and test the image?
- How do you run it with Docker Compose?
- Why publish on 127.0.0.1 and how do you add HTTPS?
- How do you ship updates and clean up?
- Questions
What should be ready before you begin?
Docker Engine with the Compose plugin on the server, and a Node.js project with a package-lock.json. If Docker is not installed yet, follow the Docker Engine tutorial or deploy the Docker app image (Ubuntu 24.04 with Docker preinstalled) from the Deploy page. Check both tools:
docker version
docker compose versionAdd your user to the docker group only if you accept that it grants root-equivalent rights; otherwise prefix commands with sudo. The examples assume an app whose npm run build writes to dist and whose entry point is dist/server.js. For a plain JavaScript app without a build step, drop the build stage.
How do you keep junk out of the build context?
Create a .dockerignore file first. It stops local node_modules, git history and secrets from being sent to the builder, which speeds up builds and keeps tokens out of image layers.
node_modules
npm-debug.log
dist
.git
.env
.env.*
Dockerfile
compose.yaml
coverageWhat does a good multi-stage Dockerfile look like?
Separate stages for production dependencies, the build, and the final runtime. Only the last one ends up in the image you run, so compilers and dev dependencies never ship.
# syntax=docker/dockerfile:1
FROM node:24-slim AS base
WORKDIR /app
COPY package.json package-lock.json ./
FROM base AS deps
RUN --mount=type=cache,target=/root/.npm npm ci --omit=dev
FROM base AS build
RUN --mount=type=cache,target=/root/.npm npm ci
COPY . .
RUN npm run build
FROM node:24-slim AS runtime
ENV NODE_ENV=production
WORKDIR /app
COPY --from=deps --chown=node:node /app/node_modules ./node_modules
COPY --from=build --chown=node:node /app/dist ./dist
COPY --chown=node:node package.json ./
USER node
EXPOSE 3000
HEALTHCHECK --interval=30s --timeout=3s --retries=3 \
CMD node -e "fetch('http://127.0.0.1:3000/health').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))"
CMD ["node", "dist/server.js"]Copying the two package files before the source means Docker reuses the cached dependency layer until the lockfile changes. The cache mounts keep npm downloads between builds. Pin the image to the major you test against, and to a digest if you want fully repeatable builds.
Why run as a non-root user, and how?
A process that runs as root inside a container is root inside that namespace, and a bug that escapes the app has far more to work with. The official Node.js images include a user named node (UID 1000), so USER node is all it takes. The --chown flags give that user ownership of the copied files.
Two consequences follow. The app cannot bind to ports below 1024, so listen on 3000 and map ports in Compose. And it can only write where it owns files: create a data directory in the Dockerfile with RUN mkdir -p /app/data && chown node:node /app/data before the USER line, or mount a volume there.
Inside the container the app must listen on 0.0.0.0, not 127.0.0.1, otherwise the port mapping has nothing to reach. Exposure to the internet is controlled by Compose instead.
How do you build and test the image?
Build with a tag, check its size, and run it once by hand.
docker build -t shop:1.0 .
docker image ls shop
docker run --rm -p 127.0.0.1:3000:3000 shop:1.0From another SSH session run curl http://127.0.0.1:3000/health. Confirm the user with docker exec -it CONTAINER id; it should print uid=1000(node). A slim-based image for a typical API usually lands in the low hundreds of megabytes. If you need native modules that compile during npm ci, install python3 make g++ with apt in the deps stage only.
How do you run it with Docker Compose?
Compose keeps the run settings in a file next to the code. Save this as compose.yaml:
services:
app:
build: .
image: shop:1.0
restart: unless-stopped
env_file: .env
ports:
- "127.0.0.1:3000:3000"
volumes:
- appdata:/app/data
read_only: true
tmpfs:
- /tmp
logging:
driver: local
options:
max-size: "10m"
volumes:
appdata:docker compose up -d --build
docker compose ps
docker compose logs -f appread_only makes the container file system immutable apart from the volume and /tmp. The local log driver with a size cap keeps logs from filling the disk; more about that in managing Docker logs. If the app needs PostgreSQL or Redis, add them as extra services in the same file and refer to them by service name. The Docker Compose guide covers multi-service files in depth.
Why publish on 127.0.0.1 and how do you add HTTPS?
Docker writes its own packet filter rules for published ports, and those are evaluated before ufw rules. A plain "3000:3000" mapping therefore opens the port to the whole internet even when ufw says it is closed. Binding to 127.0.0.1 keeps it local, and a reverse proxy on the host handles the public side.
With Caddy on the host, two lines give you automatic HTTPS:
app.example.net {
reverse_proxy 127.0.0.1:3000
}The Caddy HTTPS tutorial walks through installing it, and the Express behind Nginx guide shows the Nginx route. Open only ports 22, 80 and 443 in ufw or firewalld.
How do you ship updates and clean up?
Pull the code, rebuild and recreate the container. Compose replaces it only if the image or configuration changed.
git pull
docker compose up -d --build
docker image prune -fTag releases (shop:1.1) instead of reusing one tag, so a rollback is changing the tag back and running docker compose up -d. Rebuild regularly even without code changes, because the base image receives security fixes; docker compose build --pull fetches the newest node:24-slim. Old images and build cache pile up over time, and cleaning up Docker shows how to reclaim the space safely.
Plans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
Questions about Containerise a Node.js app
Should I use the Alpine or the slim Node.js image?
Slim is based on Debian and uses glibc, so prebuilt native modules work out of the box. Alpine is smaller but uses musl, which can force native modules to compile and occasionally behaves differently.
Do I still need PM2 inside a container?
Usually not. Docker restarts the container on failure, and you scale by running more containers. Run node directly as the main process so stop signals reach your app.
Why does my container ignore docker stop for ten seconds?
The app does not handle SIGTERM, so Docker waits and then kills it. Add a SIGTERM handler that closes the server, and avoid starting through npm start, which may not forward signals.
Where should secrets go?
In an .env file read by Compose at runtime, never in the Dockerfile or image. Keep .env in .dockerignore and .gitignore.
Can I build on my laptop and run on the server?
Yes. Build for the server architecture, push to a registry you control, and pull on the server. Building on the server itself is simpler for a single instance.