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.
Quick Explain
- ▸Docker uses containerization to isolate applications and their dependencies from the host system.
- ▸Supports building images with Dockerfiles and running containers from those images.
- ▸Provides networking, storage, and orchestration primitives to manage containers.
- ▸Integrates with CI/CD pipelines for automated builds and deployments.
- ▸Optimizes resource utilization compared to traditional virtual machines.
Core Features
- ▸Docker Engine - runtime for building and running containers
- ▸Docker Images - snapshot of an app and its dependencies
- ▸Docker Containers - running instances of images
- ▸Dockerfile - declarative instructions to build images
- ▸Docker Compose - define and run multi-container apps
Learning Path
- ▸Understand containerization concepts
- ▸Learn Docker CLI commands
- ▸Write Dockerfiles and build images
- ▸Use Docker Compose for multi-container apps
- ▸Deploy and monitor containers in production
Practical Examples
- ▸Containerize a Node.js REST API
- ▸Run a MySQL database container and connect app
- ▸Use Docker Compose to run web app + database
- ▸Build multi-stage Dockerfile for optimized image
- ▸Deploy containers to cloud platforms like AWS ECS or Azure
Comparisons
- ▸Docker vs Virtual Machines: Docker more lightweight, faster startup, shares OS kernel
- ▸Docker vs Podman: Podman daemonless, rootless; Docker mature ecosystem
- ▸Docker vs LXC: Docker focused on application containers; LXC system containers
- ▸Docker vs Kubernetes: Kubernetes orchestrates containers; Docker builds/runs them
- ▸Docker vs Singularity: Singularity designed for HPC, Docker for general-purpose apps
Strengths
- ▸Portability across environments
- ▸Resource-efficient compared to VMs
- ▸Rapid provisioning and scaling
- ▸Simplifies CI/CD pipelines
- ▸Strong ecosystem and community support
Limitations
- ▸Requires learning Docker CLI and concepts
- ▸Container isolation not as strong as full VMs for security-sensitive workloads
- ▸Persistent storage requires careful management
- ▸Networking between containers can be complex
- ▸Performance overhead when running GUI or heavy I/O applications
When NOT to Use
- ▸Applications that require full VM isolation
- ▸Legacy software tied to specific OS configurations
- ▸When minimal containerization overhead is unnecessary
- ▸GUI-heavy desktop apps
- ▸When the team lacks containerization expertise
Cheat Sheet
- ▸docker build -t myapp . - build image
- ▸docker run -p 8080:80 myapp - run container
- ▸docker ps - list running containers
- ▸docker stop <container> - stop container
- ▸docker-compose up - start multi-container app
FAQ
- ▸Is Docker open-source? -> Yes, Apache 2.0 license.
- ▸Can Docker run on Windows/macOS? -> Yes, via Docker Desktop.
- ▸Does Docker replace VMs? -> No, it complements them with lightweight containers.
- ▸Can Docker containers communicate with each other? -> Yes, via Docker networks.
- ▸How to persist data in Docker? -> Use volumes or bind mounts.
30-Day Skill Plan
- ▸Week 1: Install Docker and run hello-world container
- ▸Week 2: Write Dockerfile and build custom images
- ▸Week 3: Use Docker Compose for multi-service apps
- ▸Week 4: Integrate with CI/CD pipelines
- ▸Week 5: Deploy containers to cloud and optimize performance
Final Summary
- ▸Docker enables containerized applications for portability and consistency.
- ▸Supports building, shipping, and running containers efficiently.
- ▸Optimized for microservices, CI/CD, and cloud-native apps.
- ▸Provides tooling for orchestration, networking, and storage.
- ▸Strong ecosystem, community, and integration options.
Project Structure
- ▸Dockerfile - defines image build steps
- ▸docker-compose.yml - defines multi-container setups
- ▸src/ - application source code
- ▸configs/ - environment-specific configurations
- ▸data/ - optional persistent volumes
Monetization
- ▸Docker is open-source (Apache 2.0) with commercial support
- ▸Reduces deployment friction and operational costs
- ▸Enables rapid CI/CD for enterprise apps
- ▸Supports multi-cloud deployments efficiently
- ▸Enterprise Docker Enterprise adds advanced security and management features
Productivity Tips
- ▸Use multi-stage builds for small images
- ▸Keep containers stateless when possible
- ▸Use volumes for persistent data
- ▸Automate builds and deployments with CI/CD
- ▸Regularly prune unused images and containers
Basic Concepts
- ▸Image - immutable snapshot of an application and its dependencies
- ▸Container - running instance of an image
- ▸Volume - persistent storage for containers
- ▸Network - allows communication between containers
- ▸Dockerfile - script for building images
Official Docs
- ▸https://docs.docker.com/
- ▸Docker GitHub repository
- ▸Docker community forums and tutorials