Dockerize a Node.js App using a Distroless Image
A multi-stage Dockerfile that builds a Node.js app on Debian slim and runs it on Google's distroless image as a non-root user, then deploys it on Railway.
In a previous post, I covered the deployment of a simple Node.js application on Railway. Today, I'll explain how to dockerize the app using a distroless container image for better security, performance, and manageability.
What is a Google Distroless Image?
Google's distroless images contain your application's runtime and its dependencies, and nothing else: no package manager, no shell, no coreutils. The smallest, gcr.io/distroless/static-debian13, is about 2 MiB. The Node.js images add the Node runtime and the system libraries it needs on top of that. Fewer packages means fewer CVEs for scanners to flag and less for an attacker to work with if they get code execution: there's no shell to drop into and no curl to download a second stage. All distroless images are signed with Sigstore Cosign using keyless signing, so you can verify an image before you deploy it.
Dockerize a Simple Node.js App using a Distroless Image
I'll use my minimal Node.js web app: an Express server in app.js that serves a static page. Fork it, then add the two files below.
Because a distroless image has no shell and no npm, you can't install dependencies inside it. So the Dockerfile has two stages: the first installs production dependencies in a regular Node image, and the second copies the result into the distroless image.
A naive Dockerfile would look like this. It works, but it ships the full Debian-based node image (around 1 GB, build tools included), runs as root, and pulls whatever version node points to on the day you build:
FROM node
WORKDIR /app
COPY . .
RUN npm install
CMD ["node", "app.js"]Snyk's guides to choosing a Node.js Docker image and containerizing Node.js apps cover the reasoning in depth. The version below pins exact base image versions, so a rebuild next month gives you the same image. It builds on Debian slim rather than Alpine, because the distroless image is Debian-based (glibc) and dependencies with native code compiled on Alpine (musl) won't run on it. It installs production dependencies only, and runs as the distroless image's built-in nonroot user.

Here's the full Dockerfile:
# Stage 1: install production dependencies
FROM node:24.22.2-trixie-slim AS build
ENV NODE_ENV=production
WORKDIR /app
COPY package*.json ./
RUN npm install --omit=dev
COPY . .
# Stage 2: run on distroless as a non-root user
FROM gcr.io/distroless/nodejs24-debian13:nonroot
ENV NODE_ENV=production
WORKDIR /app
COPY --from=build /app /app
EXPOSE 3000
CMD ["app.js"]The distroless Node image sets node as its entrypoint, which is why CMD takes only the script name. The :nonroot tag runs the container as an unprivileged user (UID 65532), so there's no separate USER line. The builder uses Debian 13 ("trixie") slim to match the distroless image's Debian 13 base.
Add a .dockerignore too, so your local node_modules and git history don't end up in the build:
node_modules
.gitTo try it locally:
docker build -t nodejs-distroless .
docker run --rm -p 3000:3000 nodejs-distrolessOpen http://localhost:3000. If you need to look inside a running container while debugging, distroless has :debug-nonroot variants that add a BusyBox shell. Use them locally only, never in production.
Deploy the Dockerized Node.js App on Railway
Railway builds from a Dockerfile automatically when it finds one in the repository root. Create a new project from your fork, and Railway builds both stages and runs the distroless image. It also sets the PORT variable, which app.js already reads, and you can generate a public domain under the service's Networking settings.
If you only want the app running and don't need the Docker setup, my Node.js template deploys the same app in one click, using Railway's standard Node.js build instead of the Dockerfile.
Frequently Asked Questions
What is a distroless image? A container image that contains only your application and its runtime dependencies, with no package manager, shell or other operating system tools. Google maintains distroless images for Node.js, Java, Python and static binaries.
How do I debug a distroless container without a shell? Use the :debug or :debug-nonroot variant locally, which adds a BusyBox shell. In Kubernetes, you can attach an ephemeral debug container to the running pod with kubectl debug.
Should I use Alpine or distroless for Node.js? Distroless, if you can build on a Debian image. Alpine uses musl, which can break dependencies with native code, and it still includes a shell and package manager. Distroless images are glibc-based and ship neither.
Which Node.js versions does distroless support? Distroless publishes Node.js images for the current Node releases on Debian 13, such as gcr.io/distroless/nodejs24-debian13. Match the major version in your build stage to the one in your runtime image.