All tutorials
App DeploymentIntermediate

Docker for Web Developers: From Dockerfile to Production

Containerize any web app, use multi-stage builds, run services with Docker Compose and deploy the image.

Hostn.ME Team 10 min read 25 viewsUpdated November 22, 2026

Docker packages your app with everything it needs, so it runs the same on your laptop, a VPS or App Hosting.

1. A production-ready Dockerfile (Node.js)

# --- build stage ---
FROM node:20-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

# --- runtime stage ---
FROM node:20-alpine
WORKDIR /app
ENV NODE_ENV=production
COPY --from=build /app/package*.json ./
RUN npm ci --omit=dev
COPY --from=build /app/dist ./dist
USER node
EXPOSE 3000
CMD ["node", "dist/server.js"]

Multi-stage builds keep the final image small and free of build tools.

2. Ignore what you don't need

# .dockerignore
node_modules
.git
.env
*.log

3. Local development with Compose

# docker-compose.yml
services:
  web:
    build: .
    ports: ["3000:3000"]
    environment:
      DATABASE_URL: postgres://app:secret@db:5432/app
    depends_on: [db]
  db:
    image: postgres:16
    environment:
      POSTGRES_USER: app
      POSTGRES_PASSWORD: secret
      POSTGRES_DB: app
    volumes: ["pgdata:/var/lib/postgresql/data"]
volumes:
  pgdata:
docker compose up --build

4. Deploy

  • App Hosting: commit the Dockerfile — we detect and build it automatically.
  • VPS: install Docker, then docker compose up -d behind Nginx.

5. Good habits

  • Run as a non-root user (USER node)
  • Pin image versions (node:20-alpine, not latest)
  • Add a health check endpoint like /healthz
  • Keep secrets in environment variables, never in the image

Stuck on a step?

Our AI assistant knows every tutorial, and a real engineer is one click away.