Day-8 | Docker Advanced | Part-2
Introduction to Docker Compose
Docker Compose is a powerful tool that allows you to define and manage multi-container Docker applications. With Docker Compose, you can define your application’s services, networks, and volumes in a simple YAML file, and then run your entire application with a single command.
Basic Structure of a YAML File
A YAML file is used to configure the services, networks, and volumes that your Docker application will use. The basic structure of a YAML file is:
Steps to Install, Run & Stop Docker Compose
Install Docker Compose:
sudo apt-get install docker-compose-v2
Run the containers / yml file:
docker-compose up -d
The
-d
flag runs the containers in detached mode.Stop the containers:
docker-compose down
This command stops and removes all containers defined in your
docker-compose.yml
file.
Example of a Docker Compose YAML File
Here is an example YAML file that defines a two-tier application with a MySQL database and a Flask application:
version: '3.9'
services:
mysql:
container_name: mysql
image: mysql:5.7
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: test@123
MYSQL_DATABASE: devops
volumes:
- mysql-data:/var/lib/mysql
networks:
- twotier
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-p$MYSQL_ROOT_PASSWORD"]
interval: 10s
retries: 5
start_period: 30s
two-tier-app:
container_name: two-tier-app
build:
context: .
ports:
- "5000:5000"
environment:
MYSQL_HOST: mysql
MYSQL_USER: root
MYSQL_PASSWORD: test@123
MYSQL_DB: devops
depends_on:
mysql:
condition: service_healthy
networks:
- twotier
volumes:
mysql-data:
networks:
twotier:
driver: bridge
Multi-Stage Builds in Docker
Multi-stage builds allow you to create smaller, more efficient Docker images by separating the build process into multiple stages.
Here's an example:
# Stage 1: Build
FROM python:3.11 AS backend-builder
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
# Stage 2: Final Image
FROM python:3.11-slim
WORKDIR /app
COPY --from=backend-builder /usr/local/lib/python3.11/site-packages/ /usr/local/lib/python3.11/site-packages
COPY --from=backend-builder /app /app
CMD ["python","app.py"]
Docker Scout
Docker Scout is a tool that helps you analyze and understand the security risks in your Docker images by identifying Common Vulnerabilities and Exposures (CVEs).
Steps to Install Docker Scout:
Pull Docker Scout from Docker Hub:
docker pull docker/scout:latest
Make .docker directory:
mkdir -p $HOME/.docker/scout
Run the curl command:
curl -fsSL https://raw.githubusercontent.com/docker/scout-cli/main/install.sh -o install-scout.sh sh install-scout.sh
Login to Docker Hub using PAT:
docker login --username YOUR_USERNAME --password YOUR_PAT
Run CVE Scanning on MySQL:
docker scout cves mysql:latest
QuickScan:
docker scout quickview mysql:latest
Pushing and Pulling Images on Docker Hub
Push an Image to Docker Hub:
docker tag local-image-name dockerhub-username/repository-name:tag docker push dockerhub-username/repository-name:tag
Pull an Image from Docker Hub:
docker pull dockerhub-username/repository-name:tag
Docker Init
docker init
is a command that helps you quickly set up a Dockerized project by generating a Dockerfile
and other configuration files based on your project structure.
Conclusion
Docker Compose and multi-stage builds are essential tools in modern software development, allowing for efficient, scalable, and secure application deployment. By leveraging Docker's capabilities, I was able to create, connect, and manage containers seamlessly, contributing to a more streamlined and automated DevOps workflow.
Subscribe to my newsletter
Read articles from Dhruv Rajvanshi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by