Docker containers

Docker for Node.js Applications

Complete guide to containerizing Node.js applications with Docker.

#docker#nodejs#devops#containers
# Docker for Node.js Applications Learn how to containerize your Node.js applications with Docker. ## Creating a Dockerfile Here's a basic Dockerfile for Node.js: ```dockerfile FROM node:18-alpine WORKDIR /app COPY package*.json ./ RUN npm ci --only=production COPY . . EXPOSE 3000 CMD ["node", "dist/main.js"] ``` ## Multi-stage Builds Optimize your Docker images: ```dockerfile # Build stage FROM node:18-alpine AS builder WORKDIR /app COPY . . RUN npm ci && npm run build # Production stage FROM node:18-alpine WORKDIR /app COPY --from=builder /app/dist ./dist COPY package*.json ./ RUN npm ci --only=production CMD ["node", "dist/main.js"] ``` ## Docker Compose Orchestrate multiple containers: ```yaml version: '3.8' services: app: build: . ports: - "3000:3000" db: image: postgres:14 environment: POSTGRES_PASSWORD: password ```
Docker for Node.js Applications | Ahmad's Blog