sethserver / Programming

Deploying Python Web Applications with Docker: A Comprehensive Guide

By Seth Black Updated September 27, 2024

Brief History of Docker

I first started using Docker around 2014 when it was starting to gain popularity. I had been working with virtual machines for a while, and Docker's lightweight containers were a game-changer for me. Kernel-level virtualization was the key to Docker's success for me. Now, over a decade later I still use Docker for all my web applications. It's a great tool for not just for devs, but also sysadmins, and even the annoying infosec peeps (you know who you are - "CVE-2021-44228" makes your eye twitch).

Why Docker is So Useful

Docker eliminates the “works on my machine” problem by ensuring applications behave consistently across environments. Containers are lightweight and portable, making deployment on different infrastructures seamless. With Docker, you can scale applications, run isolated services, and maintain consistent environments.

Why Python is Great for Web Applications

Python is my top choice for web development due to its fast development cycle, even though it may not be the fastest language in terms of execution. Frameworks like Flask and Django allow developers to rapidly build robust web services.

  • Rapid Development: Python’s clean syntax speeds up development.
  • Strong Ecosystem: Extensive libraries and frameworks like Flask and Django for web services.
  • Asynchronous Capabilities: Efficient handling of I/O-bound tasks using tools like asyncio and FastAPI.

Step-by-Step Tutorial for Deploying Python Web Apps Using Docker

Step 1: Installing Docker

Start by installing Docker on your system. Follow the official Docker installation guide and verify the installation with:

docker --version

Step 2: Create a Python Web App

Let's create a basic Flask app to demonstrate. Below is the code for app.py:

from flask import Flask
app = Flask(__name__)

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

if __name__ == "__main__":
    app.run(host='0.0.0.0')
        

Add a requirements.txt file with the following:

Flask==2.0.1

Step 3: Writing the Dockerfile

Create a Dockerfile to containerize the app:


# Use the official Python image.
FROM python:3.9-slim

# Set the working directory.
WORKDIR /app

# Copy the current directory contents into the container at /app.
COPY . /app

# Install dependencies.
RUN pip install -r requirements.txt

# Expose the Flask port.
EXPOSE 5000

# Command to run the app.
CMD ["python", "app.py"]
        

Step 4: Building and Running the Docker Image

Build the Docker image:

docker build -t python-flask-app .

Then run the container:

docker run -p 5000:5000 python-flask-app

Step 5: Docker Compose for Complex Apps

For apps with multiple services (e.g., web app and database), use Docker Compose. Here’s an example docker-compose.yml file:


version: '3'
services:
  web:
    build: .
    ports:
      - "5000:5000"
  db:
    image: postgres
    environment:
      POSTGRES_USER: db_user
      POSTGRES_PASSWORD: db_pass
        

Bring up the services with:

docker-compose up

Step 6: Deploying to the Cloud

Once your app is containerized, deploy it to cloud platforms like AWS, Google Cloud, or Heroku. For AWS ECS, push the image to Amazon ECR, define tasks and services, and you're ready to go. For Heroku, use a heroku.yml file and deploy with Docker support.

Docker provides an easy, efficient way to deploy Python web applications across platforms. Its portability and scalability allow developers to build, test, and deploy with confidence.

-Sethers