Multi-stage Dockerfile - Docker Typing CST Test
Loading…
Multi-stage Dockerfile — Docker Code
Shows best practices for multi-stage builds, security considerations, and optimized Node.js container deployment.
# Multi-stage Dockerfile for Node.js application
FROM node:18-alpine AS builder
# Set working directory
WORKDIR /app
# Copy package files
COPY package*.json ./
# Install dependencies
RUN npm ci --only=production
# Copy source code
COPY . .
# Build the application
RUN npm run build
# Production stage
FROM node:18-alpine AS production
# Create non-root user
RUN addgroup -g 1001 -S nodejs && \
adduser -S nextjs -u 1001
# Set working directory
WORKDIR /app
# Copy built application from builder stage
COPY --from=builder --chown=nextjs:nodejs /app/dist ./dist
COPY --from=builder --chown=nextjs:nodejs /app/node_modules ./node_modules
COPY --from=builder --chown=nextjs:nodejs /app/package.json ./package.json
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
CMD curl -f http://localhost:3000/health || exit 1
# Switch to non-root user
USER nextjs
# Expose port
EXPOSE 3000
# Set environment variables
ENV NODE_ENV=production
ENV PORT=3000
# Start the application
CMD ["npm", "start"]Docker Language Guide
Docker is a platform for developing, shipping, and running applications inside lightweight, portable containers, enabling consistent environments across development, testing, and production.
Primary Use Cases
- ▸Packaging applications with dependencies into portable containers
- ▸Running microservices and cloud-native apps
- ▸Continuous integration/continuous deployment (CI/CD)
- ▸Environment standardization across development, testing, and production
- ▸Simplifying deployment on cloud platforms or on-premises servers
Notable Features
- ▸Lightweight containerization with OS-level isolation
- ▸Docker images for reproducible builds
- ▸Docker Compose for multi-container orchestration
- ▸Integration with container registries like Docker Hub
- ▸Networking and volume management for containers
Origin & Creator
Created by Solomon Hykes in 2013 as part of dotCloud, now maintained by Docker, Inc. and open-source community.
Industrial Note
Docker is widely used in DevOps, microservices architectures, cloud-native applications, and continuous deployment pipelines where environment consistency, scalability, and rapid delivery are essential.