Developing and Deploying Flask Applications with Docker Compose

Flask Application Setup with Docker Compose

Project File Structure:

project_root/
|-- src/
|   |-- main.py
|   |-- requirements.txt
|-- Dockerfile
|-- docker-compose.yaml

main.py (Flask Application):

from flask import Flask

web_application = Flask(__name__)

@web_application.route('/')
def index():
    return 'Flask application running inside Docker container.'

if __name__ == '__main__':
    web_application.run(host='0.0.0.0', port=8080)

requirements.txt (Dependencies):

Flask

Dockerfile (Image Definition):

# Use Python 3.8 as the base image
FROM python:3.8-slim

# Set the working directory inside the container
WORKDIR /usr/src/application

# Copy application files into the container
COPY src/ ./

# Install Python dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Declare the port the app listens on
EXPOSE 8080

# Specify the command to run the application
CMD ["python", "main.py"]

docker-compose.yaml (Service Orchestration):

services:
  web-service:
    build:
      context: .
    ports:
      - "8080:8080"

In the docker-compose.yaml file:

  • The services block defines a service named web-service.
  • The build directive instructs Docker Compose to construct an image using the Dockerfile from the current directory (.).
  • The ports configuration maps port 8080 from the container to port 8080 on the host machine, making the Flask application accessible.

To launch the application, navigate to the directory containing docker-compose.yaml and execute:

docker-compose up -d

The -d flag runs the containers in the background (detached mode). Once running, access the application at http://localhost:8080. To stop and remove the containers, use:

docker-compose down

Managing Docker Compose Image Rebuilds

Docker Compose may display a warning like Building flask-app if the service's image does not exist or requires an update due to changes in the Dockerfile or its build context (e.g., requirements.txt or appplication code). To incorporate these changes, a rebuild is necessary.

Rebuilding the Service Image

Option 1: Rebuild and Start Services Run the following command in the project directory to rebuild images and restart all services:

docker-compose up --build -d

The --build flag forces a rebuild of images before starting the containers.

Option 2: Rebuild Without Starting To rebuild the images for all defined services without starting them, use:

docker-compose build

Option 3: Target a Specific Service To rebuild the image for only one service (e.g., web-service), you can specify the service name:

docker-compose build web-service

Or, to rebuild and start just that service:

docker-compose up --build -d web-service

Key Considerations

  • Ensure all file modifications are saved before initiating a rebuild.
  • Verify that the Dockerfile and docker-compose.yaml are correct positioned and configured.
  • Rebuilding replaces the existing image for the service, ensuring the container runs with the latest application code and dependencies.

Tags: docker Docker Compose Flask containerization python

Posted on Fri, 14 Aug 2026 16:42:25 +0000 by coppercoins