π Learn how to use Docker effectively for your DevOps journey!
Introduction
Docker makes it easy to build, run, and deploy applications in a lightweight and scalable way. Whether youβre working on microservices or large-scale applications, understanding advanced Docker techniques can improve security, performance, and efficiency.
In this guide, we will take a Flask application and apply six key Docker concepts:
β Multi-Stage Builds & Distroless Images β Make your images small and secure
β Docker Hub β Store and share images
β Docker Volumes β Keep MySQL data safe
β Docker Networking β Make services talk to each other
β Docker Compose β Manage multiple containers easily
β Docker Scout β Check for security issues
By the end, youβll be able to deploy a Flask-MySQL application using Docker like a pro! π³π₯
Step 1: Multi-Stage Builds & Distroless Images
What is a Multi-Stage Build?
Multi-stage builds help reduce image size by separating the build process from the final runtime. This means your final image will only have what is needed to run the appβnothing extra!
What is a Distroless Image?
A distroless image has no package manager or shell, making it more secure and lightweight.
Example: Multi-Stage Build for Flask
1οΈβ£ Create a Flask app (app.py)
from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
return "Hello, Dockerized Flask App!"
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)
2οΈβ£ Create a Dockerfile with Multi-Stage Build & Distroless Image:
# Stage 1: Build dependencies
FROM python:3.9 AS builder
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
# Stage 2: Minimal runtime environment
FROM gcr.io/distroless/python3
WORKDIR /app
COPY --from=builder /usr/local/lib/python3.9/site-packages /usr/local/lib/python3.9/site-packages
COPY app.py .
CMD ["python", "app.py"]
β Why use this?
β Smaller image β Faster downloads π
β No unnecessary tools β More security π
β Efficient & optimized deployment β
Step 2: Docker Hub - Storing & Sharing Your Image
What is Docker Hub?
Docker Hub is like Google Drive for Docker imagesβit lets you store, share, and pull container images anywhere.
How to Push an Image to Docker Hub?
1οΈβ£ Login to Docker Hub:
docker login -u your-dockerhub-username
2οΈβ£ Tag your image:
docker tag flask-app yourdockerhub/flask-app:v1.0
3οΈβ£ Push your image to Docker Hub:
docker push yourdockerhub/flask-app:v1.0
4οΈβ£ Pull and run your image anywhere:
docker pull yourdockerhub/flask-app:v1.0
docker run -p 5000:5000 yourdockerhub/flask-app:v1.0
β Why use this?
β Store your images in one place π¦
β Easily share across different machines π
β Version control & backups π
Step 3: Docker Volumes - Keep MySQL Data Safe
What are Docker Volumes?
Volumes help store MySQL data permanently, so it doesnβt disappear when a container stops.
Run MySQL with a Volume
docker volume create mysql_data
docker run -d \
--name mysql_container \
-e MYSQL_ROOT_PASSWORD=root \
-e MYSQL_DATABASE=mydb \
-v mysql_data:/var/lib/mysql \
mysql:latest
β Why use this?
β Prevents data loss π
β Makes backups easy π
β Works even after container restarts π₯
Step 4: Docker Networking - Connecting Flask & MySQL
What is Docker Networking?
Networking lets containers talk to each other securely.
Create a Network & Connect Flask & MySQL
docker network create app_network
docker run -d --name mysql_container --network app_network \
-e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=mydb mysql:latest
docker run -d --name flask_app --network app_network -p 5000:5000 flask-app
β Why use this?
β Secure communication between services π
β No need for exposing unnecessary ports β
Step 5: Docker Compose - Managing Multiple Containers
What is Docker Compose?
It helps run multiple services (Flask & MySQL) with a single command.
Create docker-compose.yml
version: '3.8'
services:
mysql:
image: mysql:latest
container_name: mysql_db
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: flask_db
volumes:
- mysql_data:/var/lib/mysql
networks:
- app_network
flask:
build: .
container_name: flask_app
depends_on:
- mysql
environment:
DATABASE_HOST: mysql_db
DATABASE_NAME: flask_db
networks:
- app_network
ports:
- "5000:5000"
volumes:
mysql_data:
networks:
app_network:
Run Everything with One Command
docker-compose up -d
β Why use this?
β One command for everything π₯
β Works the same on all systems π
Step 6: Docker Scout - Check Security Issues
What is Docker Scout?
Docker Scout helps find security vulnerabilities in container images.
Check Flask App for Security Issues
1οΈβ£ Install Docker Scout
docker scout quickview yourdockerhub/flask-app:v1.0
2οΈβ£ List vulnerabilities
docker scout cves yourdockerhub/flask-app:v1.0
3οΈβ£ Check security recommendations
docker scout recommendations yourdockerhub/flask-app:v1.0
β Why use this?
β Find security issues early π
β Keep containers safe & updated π
Conclusion
By using advanced Docker techniques, you can deploy applications efficiently and securely.
β Key Takeaways:
β Multi-Stage Builds & Distroless Images β Faster & smaller images
β Docker Hub β Store & share your images
β Docker Volumes β Keep MySQL data safe
β Docker Networking β Secure container communication
β Docker Compose β Manage multiple services easily
β Docker Scout β Find security vulnerabilities
π Want to build scalable & secure apps? Start using these Docker techniques today! π³π₯