Mastering Docker for DevOps: A Simple Guide with a Flask App

Mastering Docker for DevOps: A Simple Guide with a Flask App

Β·

4 min read

πŸš€ 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! 🐳πŸ”₯

Β