AceDevHub
Advanced Node.js Interview QuestionsAdvancedConcept

Node.js · Question 82

What are Docker basics for Node.js applications?

Direct answer

Docker packages Node apps in images — Dockerfile multi-stage build (install → build → slim runtime), docker-compose for api/web/postgres/redis, .dockerignore node_modules, non-root USER, healthcheck on /health; AceDevHub local stack uses ports 4000/3000/5433/6379.

Containers give reproducible environments — same Node version, same env vars, same service topology from laptop to Hetzner production.

  • Multi-stage Dockerfile — builder installs deps + compiles TS; runtime copies dist only.
  • docker-compose.yml — services: api, web, worker, postgres, redis; profiles for caddy.
  • Volumes — postgres data persisted; bind mount source only in dev.
  • Networking — web calls http://api:4000 inside compose network.
Dockerfile
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build --workspace @acedevhub/api

FROM node:20-alpine
WORKDIR /app
ENV NODE_ENV=production
COPY --from=builder /app/apps/api/dist ./dist
USER node
EXPOSE 4000
HEALTHCHECK CMD wget -qO- http://localhost:4000/health || exit 1
CMD ["node", "dist/server.mjs"]