Skip to content

GaffarProjects/flask-docker-app

Repository files navigation

🐳 Docker Flask Application

A simple, production-ready Flask web application containerized with Docker, demonstrating DevOps best practices.

πŸ“‹ Features

  • βœ… Lightweight Python Flask web application
  • βœ… Multi-stage Docker build for optimized image size
  • βœ… Docker Compose for easy orchestration
  • βœ… Non-root user for enhanced security
  • βœ… Health check endpoint
  • βœ… RESTful API endpoints
  • βœ… Production-ready with Gunicorn
  • βœ… Makefile for common commands

πŸ—οΈ Project Structure

docker-flask-app/
β”œβ”€β”€ app/
β”‚   └── app.py              # Main Flask application
β”œβ”€β”€ tests/                   # Test directory (for future tests)
β”œβ”€β”€ .dockerignore           # Docker ignore file
β”œβ”€β”€ .gitignore              # Git ignore file
β”œβ”€β”€ docker-compose.yml      # Docker Compose configuration
β”œβ”€β”€ Dockerfile              # Multi-stage Dockerfile
β”œβ”€β”€ Makefile                # Common commands
β”œβ”€β”€ README.md               # This file
└── requirements.txt        # Python dependencies

πŸš€ Quick Start

Prerequisites

Running the Application

  1. Clone or download this project

  2. Build and run with Docker Compose:

    docker-compose up -d
  3. Or use the Makefile:

    make run-build
  4. Access the application:

πŸ› οΈ Available Commands

Using Makefile (Recommended)

make help          # Show all available commands
make build         # Build the Docker image
make run           # Run the container
make run-build     # Build and run
make stop          # Stop the container
make restart       # Restart the container
make logs          # View logs
make shell         # Open shell in container
make clean         # Remove container and image
make test          # Test the health endpoint

Using Docker Compose

docker-compose up -d              # Start in detached mode
docker-compose down               # Stop and remove containers
docker-compose logs -f            # Follow logs
docker-compose ps                 # List containers
docker-compose restart            # Restart services

Using Docker Directly

# Build image
docker build -t docker-flask-app .

# Run container
docker run -d -p 5000:5000 --name flask-app docker-flask-app

# View logs
docker logs -f flask-app

# Stop container
docker stop flask-app

# Remove container
docker rm flask-app

πŸ“‘ API Endpoints

Method Endpoint Description
GET / Homepage with info
GET /api/health Health check endpoint
GET /api/info System information

πŸ”§ Configuration

Environment variables can be set in docker-compose.yml:

environment:
  - ENVIRONMENT=development  # or production
  - PORT=5000

πŸ§ͺ Testing

Test the health endpoint:

curl http://localhost:5000/api/health

Expected response:

{
  "status": "healthy",
  "service": "docker-flask-app"
}

πŸ“¦ Docker Best Practices Implemented

  1. Multi-stage builds - Smaller final image size
  2. Non-root user - Enhanced security
  3. Layer caching - Faster builds
  4. .dockerignore - Exclude unnecessary files
  5. Health checks - Container health monitoring
  6. Explicit base image versions - Reproducible builds
  7. Minimal base image - Using slim Python image

🚒 Deployment

Push to Docker Hub

# Tag the image
docker tag docker-flask-app yourusername/docker-flask-app:v1.0

# Login to Docker Hub
docker login

# Push the image
docker push yourusername/docker-flask-app:v1.0

Run from Docker Hub

docker run -d -p 5000:5000 yourusername/docker-flask-app:v1.0

πŸ“š Learning Resources

This project demonstrates:

  • Docker containerization
  • Multi-stage Docker builds
  • Docker Compose orchestration
  • Python Flask web framework
  • RESTful API design
  • DevOps best practices

πŸ”„ Next Steps to Enhance

  • Add unit tests with pytest
  • Implement CI/CD pipeline (GitHub Actions)
  • Add database (PostgreSQL/Redis)
  • Kubernetes manifests
  • Monitoring with Prometheus
  • Logging with ELK stack
  • Add Nginx as reverse proxy

πŸ“ License

MIT License - feel free to use this project for learning and portfolio purposes!

πŸ‘€ Author

Your Name - Your LinkedIn

🀝 Contributing

This is a learning project. Feel free to fork and modify!


Perfect for: DevOps portfolio, Docker learning, interview preparation, LinkedIn showcase

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors