Docker Compose for Flask: A Comprehensive Guide 📝

beginner
16 min

Docker Compose for Flask: A Comprehensive Guide 📝

Welcome to this detailed tutorial on using Docker Compose with Flask! In this lesson, we'll explore how to create and manage Flask applications using Docker Compose, making it easier to deploy and scale your projects. 🎯

By the end of this tutorial, you'll be able to:

  1. Set up a Flask project using Docker Compose
  2. Understand Docker Compose files and their structure
  3. Build, run, and stop Docker Compose services
  4. Use multi-service Docker Compose files for real-world projects

Let's get started! 🚀

Prerequisites 📝

Before diving into the Docker Compose for Flask tutorial, make sure you have the following prerequisites:

  • Basic knowledge of Python and Flask
  • Familiarity with Docker (optional but recommended)
  • A text editor or IDE of your choice (e.g., Visual Studio Code, Atom, PyCharm)

Creating a Flask Application 💡

First, let's create a simple Flask application. Create a new directory for your project and navigate to it:

bash
mkdir flask_docker_compose cd flask_docker_compose

Next, create a new file named app.py and paste the following code:

python
from flask import Flask, jsonify app = Flask(__name__) @app.route('/') def hello(): return jsonify({'message': 'Hello, Docker!'}) if __name__ == '__main__': app.run(debug=True)

This code creates a basic Flask application that responds with a JSON message when accessed at the root URL.

Setting Up Docker Compose 💡

Now, let's add a Dockerfile to our project to create a Docker image for our Flask application:

Dockerfile
FROM python:3.9 WORKDIR /app COPY . /app RUN pip install flask EXPOSE 5000 CMD ["python", "app.py"]

Next, create a docker-compose.yml file:

yaml
version: "3.8" services: web: build: . ports: - 5000:5000 command: python app.py

This docker-compose.yml file defines a single service called web that uses the Dockerfile in the current directory to build an image, exposes port 5000, and runs our Flask application.

Building, Running, and Stopping the Application 💡

Now, let's try building and running our Docker Compose application:

bash
docker-compose up --build

This command will build the Docker image, start the service, and make our Flask application accessible at http://localhost:5000.

To stop the application, press Ctrl+C in the terminal where the docker-compose up command is running.

Multi-service Docker Compose Files 💡

In real-world projects, you may have multiple services, such as a database, queue, or cache. To manage these services, we can use multi-service Docker Compose files.

Create a new file named docker-compose-multi.yml:

yaml
version: "3.8" services: web: build: . ports: - 5000:5000 command: python app.py db: image: postgres:13.2-alpine environment: POSTGRES_PASSWORD: mysecretpassword POSTGRES_DB: mydatabase redis: image: redis:6.0-alpine

In this example, we added a PostgreSQL database (db) and Redis cache (redis) to our application. To build and run the services, use the following command:

bash
docker-compose -f docker-compose-multi.yml up --build

Now you have a better understanding of how to use Docker Compose with Flask. Let's test your knowledge with a quick quiz!

Quick Quiz
Question 1 of 1

What command is used to build and run the services in a multi-service Docker Compose file?