Simple Docker Project - Containerized Web App
A beginner-friendly Docker project demonstrating containerization of a simple web application with Dockerfile and docker-compose
Simple Docker Project - Containerized Web App
Overview
This project demonstrates how to containerize a simple web application using Docker. We’ll create a basic Python Flask app and package it with Docker, including a Dockerfile and docker-compose configuration for easy deployment.
Project Structure
1
2
3
4
5
6
7
docker-project/
├── app/
│ ├── app.py
│ └── requirements.txt
├── Dockerfile
├── docker-compose.yml
└── README.md
Application Code
app/app.py
A simple Flask web application:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return '<h1>Hello from Docker!</h1><p>This app is running inside a container.</p>'
@app.route('/health')
def health():
return {'status': 'healthy'}, 200
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)
app/requirements.txt
Flask==3.0.0
Dockerfile
Create a Dockerfile in the project root:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Use Python 3.11 slim image as base
FROM python:3.11-slim
# Set working directory
WORKDIR /app
# Copy requirements first for better caching
COPY app/requirements.txt .
# Install dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Copy application code
COPY app/ .
# Expose port 5000
EXPOSE 5000
# Run the application
CMD ["python", "app.py"]
Docker Compose
Create docker-compose.yml for easier management:
1
2
3
4
5
6
7
8
9
10
11
12
version: '3.8'
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- ./app:/app
environment:
- FLASK_ENV=development
restart: unless-stopped
Usage
Build and Run with Docker
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Build the image
docker build -t docker-web-app .
# Run the container
docker run -d -p 5000:5000 --name web-app docker-web-app
# Check if it's running
docker ps
# View logs
docker logs web-app
# Stop and remove
docker stop web-app
docker rm web-app
Using Docker Compose
1
2
3
4
5
6
7
8
9
10
11
# Build and start services
docker-compose up -d
# View logs
docker-compose logs -f
# Stop services
docker-compose down
# Rebuild after changes
docker-compose up -d --build
Testing
Once the container is running, test the application:
1
2
3
4
5
# Test main endpoint
curl http://localhost:5000
# Test health endpoint
curl http://localhost:5000/health
Or open http://localhost:5000 in your browser.
Key Docker Concepts Demonstrated
- Base Images: Using official Python image
- Layer Caching: Copying requirements.txt before app code for better build caching
- Port Mapping: Exposing container port 5000 to host
- Volume Mounting: Using volumes for development (hot reload)
- Multi-stage Builds: Can be extended for production optimization
Next Steps
- Add a database service (PostgreSQL/MySQL) to docker-compose.yml
- Implement multi-stage builds for smaller production images
- Add health checks and resource limits
- Set up CI/CD pipeline for automated builds
- Use Docker networks for service communication
Resources
This post is licensed under CC BY 4.0 by the author.