DevOps-Containerization

Dockerized Flask Application


Production-Ready Containerized Python Web Application
Built with Docker | Optimized with Multi-Stage Builds | Published to Docker Hub


Table of Contents


Project Overview

This project demonstrates full lifecycle containerization of a Python Flask web application using Docker.

It includes:

The result is a reproducible, portable, and production-ready containerized application.


Architecture

Application Code

Dockerfile

Docker Image

Docker Container

Docker Hub Registry


Technology Stack


Project Structure

Docker-Essentials/
│
├── app.py
├── requirements.txt
├── Dockerfile
├── Dockerfile.multistage
└── .dockerignore

Application Source Code

app.py

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return "Hello from Docker!"

@app.route('/health')
def health():
    return "OK"

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

requirements.txt

Flask==2.3.3

Single-Stage Docker Implementation

Dockerfile

FROM python:3.9-slim

WORKDIR /app

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY app.py .

EXPOSE 5000

CMD ["python", "app.py"]

Build Image

docker build -t flask-essentials .

Run Container

docker run -d -p 5000:5000 --name flask-container flask-essentials

Multi-Stage Docker Implementation

Dockerfile.multistage

# Stage 1: Builder
FROM python:3.9-slim AS builder

WORKDIR /app
COPY requirements.txt .

RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"

RUN pip install --no-cache-dir -r requirements.txt

# Stage 2: Runtime
FROM python:3.9-slim

WORKDIR /app

COPY --from=builder /opt/venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"

COPY app.py .

RUN useradd -m appuser
USER appuser

EXPOSE 5000

CMD ["python", "app.py"]

Build Multi-Stage Image

docker build -f Dockerfile.multistage -t flask-essential-multi .

Image Lifecycle Management

docker images
docker history flask-essentials
docker inspect flask-essentials
docker rmi flask-essentials
docker image prune

Container Lifecycle Management

docker ps
docker ps -a
docker logs flask-container
docker stop flask-container
docker start flask-container
docker rm flask-container
docker container prune

Image Tagging & Versioning Strategy

docker tag flask-essentials nitanshutak/flask-essentials:1.0
docker tag flask-essentials nitanshutak/flask-essentials:latest
docker tag flask-essential-multi nitanshutak/flask-essentials:production

Versioning Strategy:


Publishing to Docker Hub

Login

docker login

Push

docker push nitanshutak/flask-essentials:1.0
docker push nitanshutak/flask-essentials:latest
docker push nitanshutak/flask-essentials:production

Pull & Deploy Anywhere

docker pull nitanshutak/flask-essentials:latest
docker run -d -p 5000:5000 nitanshutak/flask-essentials:latest

Security Best Practices Implemented


Optimization Techniques Used


Complete Command Reference

docker build -t flask-essentials .
docker build -f Dockerfile.multistage -t flask-essential-multi .
docker run -d -p 5000:5000 --name flask-container flask-essentials
docker logs flask-container
docker tag flask-essentials nitanshutak/flask-essentials:1.0
docker push nitanshutak/flask-essentials:1.0
docker pull nitanshutak/flask-essentials:latest
docker stop flask-container
docker rm flask-container

Conclusion

This repository represents a complete Docker containerization workflow including development, optimization, versioning, publishing, and deployment.

The application is: