Running a Flask Application with Docker: A Simple Guide

0

Docker has become an essential tool in modern development environments. If you can run and deploy web frameworks like Flask using Docker, the development and deployment process becomes significantly simplified. If you’re looking to easily manage complex development environments, Docker is a highly effective tool. In this article, we’ll explore how to set up and run a Flask application using Docker.

1. Project Directory Structure

First, you need to prepare a directory where the Flask application will run in a Docker environment. This directory will include the Flask code and Docker configuration files.

mkdir flask-docker-app
cd flask-docker-app

In this directory, you’ll create and manage the following files:

  • `app.py`: The main code for the Flask application.
  • `requirements.txt`: A file that defines Flask and its dependencies.
  • `Dockerfile`: A configuration file for building the Docker image.

2. Writing the Flask Application Code

First, create the `app.py` file and write a simple Flask application code. This code will run inside the Docker container.

nano app.py

Enter the following simple code:

from flask import Flask

app = Flask(__name__)

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

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

This code will display the message “Hello, Docker!” when accessing the “/” route. It’s a very simple example, but it works perfectly in a Docker environment.

3. Creating the `requirements.txt` File

To manage the necessary packages, including Flask, create a `requirements.txt` file.

nano requirements.txt

Enter the following content:

Flask==2.3.2

This file will be used to install Flask inside the Docker container.

4. Writing the Dockerfile

Now, let’s write a `Dockerfile` that defines the configuration for building the Docker image.

nano Dockerfile

Enter the following content:

# Use the slim version of Python 3.9
FROM python:3.9-slim

# Set the working directory to /app
WORKDIR /app

# Copy the current directory’s files into the container
COPY . /app

# Install required packages
RUN pip install --no-cache-dir -r requirements.txt

# Run the application
CMD ["python", "app.py"]

This `Dockerfile` is based on Python 3.9 and prepares the container for installing and running Flask.

5. Building the Docker Image

Once the file setup is complete, you can build the Docker image. Use the following command to build the image:

docker build -t flask-docker-app .

This command builds an image named `flask-docker-app`.

6. Running the Docker Container

Now, let’s run the Docker image. Use the following command to run the container:

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

This command does the following:

  • `-d`: Runs the container in the background.
  • `-p 5000:5000`: Maps port 5000 of your local machine to port 5000 of the container.

Now, if you access `http://localhost:5000` in your browser, you should see the “Hello, Docker!” message.

7. Managing with Docker Compose (Optional)

If you need to manage more complex settings or multiple containers, you can use `docker-compose`. Create a `docker-compose.yml` file to make container management easier.

nano docker-compose.yml

Enter the following content:

version: '3'
services:
  web:
    build: .
    ports:
      - "5000:5000"

Now, you can run the application using the `docker-compose` command:

docker-compose up -d

Conclusion

We’ve explored how to easily set up and run a Flask application with Docker. Docker is a powerful tool that simplifies the development environment and streamlines the deployment process, especially when running lightweight frameworks like Flask inside a container. Now, follow along with this process and experience the joy of efficiently managing your development environment.

Leave a Reply