Your security scanner flags 437 vulnerabilities on the production image. You triage and find that 412 of them are in the base operating system — bash, perl, package managers, GNU coreutils — that your Node.js application has never called and never will. The fix is not “patch faster”; the fix is “stop shipping a full Linux distribution as a runtime environment for a single process.” A hardened container image is one that contains exactly your application and its runtime, runs as a non-root user, drops every capability the kernel will let you drop, and treats its filesystem as read-only. Each of those four properties closes a class of attack.
What’s happening
The default Docker image pattern — FROM ubuntu:22.04, install your stack, copy code, run as root — produces an artifact with a 700 MB attack surface for a 50 MB application. The base image has package managers (so a compromised process can install more tools), a shell (so it can chain commands), a writable root filesystem (so it can drop binaries), capabilities like CAP_NET_ADMIN and CAP_SYS_ADMIN (so it can manipulate networking and devices), and runs as UID 0 (so file permissions inside the container are a fiction). Almost none of this is required to run your Node, Python, or Go binary.
The four hardening dimensions:
1. Base image. Choose the smallest image that runs your binary. The progression from worst to best:
ubuntu:22.04ornode:22(full distros) — fine for builds, never for runtime.alpine(musl, busybox) — small, has package manager. Good for “minimal but debuggable.”distroless(Google) orchainguard/static— no shell, no package manager, no anything but your runtime. Best for production.scratch— nothing at all. Static binaries only.
2. Non-root user. A process running as root inside the container can do less than you’d think (the platform’s default seccomp profile blocks most things), but “less” is not “nothing.” Run as a numeric UID > 10000 with a matching read-only home directory. Use USER in the Dockerfile and make sure the platform’s runAsNonRoot: true policy actually applies.
3. Dropped capabilities. Linux capabilities are a granular permissions model — most processes need exactly zero of them. The platform should drop all and add back only what is strictly required (often nothing for a web app, sometimes NET_BIND_SERVICE if you bind to a port < 1024 — which you should not, because the load balancer is in front of you).
4. Read-only root filesystem. The container’s filesystem should be read-only at runtime, with tmpfs mounts for the small number of paths the app legitimately writes to (logs, sockets, sometimes /tmp). Writable root is what lets a compromise drop new binaries or modify configuration.
A hardened image is also a smaller image, which means faster pulls, faster scaling, faster cold-starts, and fewer CVE alerts to triage.
How to verify
Pull the image and inspect what’s inside:
# Image size and layer count
docker images your-org/app:latest --format '{{.Size}}'
docker history your-org/app:latest --format 'table {{.CreatedBy}}\t{{.Size}}'
# User the image runs as
docker inspect your-org/app:latest --format='{{.Config.User}}'
# Should NOT be empty or "root" or "0"
# What binaries exist inside
docker run --rm --entrypoint sh your-org/app:latest -c \
'ls /bin /sbin /usr/bin /usr/sbin 2>/dev/null | wc -l'
# A distroless image will fail because /bin/sh does not exist — that is the right answer
# CVE scan
trivy image --severity HIGH,CRITICAL your-org/app:latest
grype your-org/app:latest -o table | head -30
Verify the runtime constraints actually apply. From inside the container:
id
# uid=65532 gid=65532 — NOT 0
cat /proc/self/status | grep -E '^Cap(Inh|Prm|Eff|Bnd|Amb)'
# CapEff should be 0000000000000000 (no effective capabilities)
mount | grep ' / '
# Should show "ro,..." — read-only root filesystem
touch /tmp/canary && rm /tmp/canary
echo $?
# 0 — tmpfs writes work
touch /canary 2>&1
# touch: cannot touch '/canary': Read-only file system
If any of those don’t match the expected output, the policy is documented but not enforced.
The fix
A reference Dockerfile that gets the four properties right. This is for a Node.js app; the pattern works for Python (gcr.io/distroless/python3-debian12), Go (gcr.io/distroless/static-debian12), Java (gcr.io/distroless/java21-debian12), and most others.
# syntax=docker/dockerfile:1.6
# ---- Build stage ----
FROM node:22-alpine AS build
WORKDIR /app
# Install deps as a separate layer for build cache friendliness
COPY package.json package-lock.json ./
RUN --mount=type=cache,target=/root/.npm \
npm ci --omit=dev --no-audit --no-fund
# Copy and build the app
COPY src ./src
COPY tsconfig.json ./
RUN npm run build && \
npm prune --omit=dev
# ---- Runtime stage ----
# Distroless — no shell, no package manager, just node + libc + your app
FROM gcr.io/distroless/nodejs22-debian12:nonroot
WORKDIR /app
# Distroless nonroot is uid=65532 — matches our verification step
COPY --from=build --chown=65532:65532 /app/node_modules ./node_modules
COPY --from=build --chown=65532:65532 /app/dist ./dist
COPY --from=build --chown=65532:65532 /app/package.json ./package.json
# Drop a healthcheck — distroless has no curl, so use the runtime
HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
CMD ["node", "-e", "fetch('http://localhost:3000/health').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))"]
# Non-root by default in distroless:nonroot, but make it explicit
USER 65532:65532
EXPOSE 3000
ENTRYPOINT ["/nodejs/bin/node"]
CMD ["dist/index.js"]
The platform runtime policy — drop capabilities, read-only filesystem, non-root, no privilege escalation. In Kubernetes/Jelastic Pod or equivalent PaaS YAML:
# pod-security.yaml — applied at the namespace / environment level
securityContext:
runAsNonRoot: true
runAsUser: 65532
runAsGroup: 65532
readOnlyRootFilesystem: true
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
seccompProfile:
type: RuntimeDefault
# Writable tmpfs mounts for paths the app actually needs to write
volumes:
- name: tmp
emptyDir:
medium: Memory
sizeLimit: 64Mi
- name: app-cache
emptyDir:
medium: Memory
sizeLimit: 256Mi
containers:
- name: app
image: your-org/app:1.4.7
volumeMounts:
- name: tmp
mountPath: /tmp
- name: app-cache
mountPath: /app/.cache
resources:
requests:
cpu: 250m
memory: 256Mi
limits:
cpu: 1000m
memory: 512Mi
If you are on Jelastic and need to apply the equivalent at the environment level, set the layer’s runtime properties via the platform API or the JPS manifest. The platform may require the usercustomgw or equivalent feature for cap_drop=ALL; check what your tier supports before promising it on prod.
CI gate — fail the build if the image regresses:
# .github/workflows/image-policy.yml — fragment
- name: Scan for HIGH/CRIT vulns
run: trivy image --exit-code 1 --severity HIGH,CRITICAL your-org/app:${{ github.sha }}
- name: Check no root
run: |
USER=$(docker inspect --format='{{.Config.User}}' your-org/app:${{ github.sha }})
[ -n "$USER" ] && [ "$USER" != "root" ] && [ "$USER" != "0" ] \
|| (echo "Image runs as root — FAIL" && exit 1)
- name: Check no shell
run: |
docker run --rm --entrypoint sh your-org/app:${{ github.sha }} -c 'echo ok' \
&& (echo "Image has a shell — FAIL" && exit 1) \
|| echo "OK — no shell"
One operational note: a distroless image is harder to debug live (no shell, no curl). The pattern for triage is kubectl debug / platform exec with an ephemeral debug container that has busybox, not baking debug tools into the production image. That is the right tradeoff — a debuggable production image is also a more compromisable production image.
For a managed PaaS where hardened base images are the default and image-policy gates are wired into the deploy pipeline, see our Cloud PaaS tier. We pair it with deployment management so the policy lives in the pipeline, not in a wiki nobody reads.