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

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

?? 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! ????

Hari Narayan Gupta

MERN Stack Developer | Database Designer | DevOps Enthusiast | CI/CD Pipeline | Building Scalable & Secure Web Solutions |

2 周

Useful tips

回复

要查看或添加评论,请登录

Amit Singh的更多文章