?? Mastering Docker file, Docker Compose & Multi-Stage Builds – Step by Step
Hirenkumar G.
DevOps Engineer | Multi-Platform Expert (Windows/Linux/Mac) | VMware, AWS, Azure, Docker,K8S Terraform | CI/CD (Jenkins, Git/GitHub) | Technical Content Writer | Freelancer
?? Introduction
Docker makes it easy to containerize applications, but managing multiple services and optimizing image sizes can be challenging. This is where Docker Compose and Multi-Stage Builds come in.
In this guide, we will learn: ? How to write a docker-compose.yml file ? How to create a multi-stage Dockerfile ? Best practices to keep Docker images lightweight and efficient
By the end, you’ll be able to manage multi-container applications with ease and build optimized images for production. Let’s get started! ??
?? What is Docker Compose?
Docker Compose is a tool that helps manage multiple containers using a simple YAML configuration file. Instead of running multiple docker run commands, you can define everything in a single file and start all services with a single command.
?? Benefits of Docker Compose
?? Simplifies multi-container application management ?? Defines networks, volumes, and dependencies in one place ?? Makes deployment and scaling easier
?? Writing a docker-compose.yml File
Here’s an example of a Node.js + Nginx setup:
version: '3.8'
services:
web:
image: nginx:alpine
ports:
- "8080:80"
volumes:
- ./webapp:/usr/share/nginx/html
networks:
- app-network
backend:
build: ./backend
command: node server.js
volumes:
- ./backend:/app
networks:
- app-network
networks:
app-network:
driver: bridge
?? Explanation
?? Running Docker Compose
To start the services, use:
docker-compose up -d
This runs everything in detached mode (-d).
To stop all containers, run:
docker-compose down
?? What are Multi-Stage Builds?
Multi-stage builds help create smaller and more efficient Docker images by separating the build and production environments into multiple stages.
?? Why Use Multi-Stage Builds?
? Smaller images – Only necessary files are included ? Faster deployments – Less data to transfer ? Better security – No extra dependencies in the final image
?? Writing a Multi-Stage Dockerfile
Here’s a multi-stage Dockerfile for a React app, using Node.js to build and Nginx to serve it.
领英推荐
# Stage 1: Build
FROM node:18 AS builder
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm install
COPY . .
RUN npm run build
# Stage 2: Production
FROM nginx:alpine
COPY --from=builder /app/build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
?? Explanation
?? Running the Multi-Stage Build
To build the image, run:
docker build -t my-react-app .
Now, run a container:
docker run -p 3000:80 my-react-app
Your application is now running on https://localhost:3000! ??
?? Conclusion
?? Docker Compose simplifies multi-container management. ?? Multi-Stage Builds optimize images for production. ?? Combining both techniques makes applications more efficient and scalable.
Simple Docker file example
# Use an official image as a base
FROM ubuntu:latest
# Set the maintainer information
LABEL maintainer="[email protected]"
# Update the package list and install necessary packages
RUN apt-get update && apt-get install -y curl
# Set the working directory inside the container
WORKDIR /app
# Copy local files to the container
COPY . /app
# Define the command to run when the container starts
CMD ["echo", "Container is running successfully!"]
This basic Dockerfile does the following:
Quick Recap ??
?? Dockerfile – Defines how to build a container image. ?? Multi-Stage Builds – Optimize images by reducing size and dependencies. ?? Docker Compose – Manages multiple containers easily with a single YAML file.
Example Implementation
Dockerfile (Multi-Stage Build)
# Stage 1: Build the application
FROM maven:3.8.6-openjdk-11 AS builder
WORKDIR /app
COPY . .
RUN mvn clean package -DskipTests
# Stage 2: Create a lightweight runtime image
FROM openjdk:11-jre-slim
WORKDIR /app
COPY --from=builder /app/target/app.jar app.jar
CMD ["java", "-jar", "app.jar"]
docker-compose.yml
version: '3.8'
services:
app:
build: .
ports:
- "8080:8080"
depends_on:
- db
db:
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: app_db
Key Benefits
? Smaller Image – Multi-stage builds keep only necessary files. ? Easier Management – Docker Compose handles multi-container applications. ? Faster Deployment – Efficient image builds and orchestration.
?? Aspiring DevOps Engineer | ?? Cloud Computing & Automation Enthusiast | ??? Docker, Kubernetes, AWS | ?? CI/CD Expert | Cloud Engineer | DevSecOps |?? Building Scalable, Efficient Systems
1 个月Insightful Hirenkumar G.