Deploying a Python Flask Application with Docker

Deploying a Python Flask Application with Docker

ยท

3 min read

Title: Deploying a Python Flask Application with Docker

Description: In this project, you will learn how to use Docker to deploy a simple Python Flask web application. Flask is a popular web framework that allows you to easily build web applications using Python.

The project will cover the following steps:

  1. Building a Docker image for the Flask application

  2. Running the Flask application inside a Docker container

  3. Exposing the Flask application to the host machine

  4. Using Docker Compose to orchestrate multiple containers (optional)

    First, we need to create a simple Flask application. You can create a new directory for this project and create a Python file called app.py inside it.

from flask import Flask

app = Flask(__name__)

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

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')

This application defines a single route / that returns a simple "Hello, World!" message.

Make sure to install Flask using the pip command in your virtual environment.

pip install Flask

You can run this application using the following command:

python app.py

Open your browser and go to http://localhost:5000 to see the "Hello, World!" message.

Step 2: Building a Docker Image

Now, we need to create a Dockerfile that will define how our Docker image should be built. Create a new file called Dockerfile in the same directory as app.py.

FROM python:3.9-slim-buster

WORKDIR /app

COPY requirements.txt requirements.txt
RUN pip3 install --no-cache-dir -r requirements.txt

COPY . .

CMD ["python", "app.py"]

This Dockerfile starts with a base image of Python 3.9, sets the working directory to /app, copies the requirements.txt file into the container, installs the dependencies using pip, copies the entire project directory into the container, and sets the command to run the Flask application.

We also need to create a requirements.txt file in the same directory as app.py to specify the dependencies for the Flask application.

Flask==2.0.2

Now, we can build the Docker image using the following command:

docker build -t my-flask-app .

This command will build the Docker image and tag it with the name my-flask-app.

Step 3: Running the Docker Container

Now, we can run the Docker container using the following command:

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

This command will start the Docker container and map port 5000 on the host machine to port 5000 inside the container.

Open your browser and go to http://localhost:5000 to see the "Hello, World!" message again.

Step 4: Using Docker Compose (Optional)

If you want to orchestrate multiple containers, you can use Docker Compose. Create a new file called docker-compose.yml in the same directory as app.py.

codeversion: "3.9"
services:
  app:
    build: .
    ports:
      - "5000:5000"
    environment:
      FLASK_APP: app.py
      FLASK_ENV: development

This file specifies a service called app that builds a Docker image from the current directory (where the Dockerfile and requirements file are located), exposes port 5000, and sets two environment variables (FLASK_APP and FLASK_ENV) for the Flask application.

You can start the application using Docker Compose using the following command:

docker-compose up

This command will start the application and display the logs in the terminal. Open your browser and go to http://localhost:5000 to see the "Hello, World!" message again.

That's it! You have successfully deployed a Python Flask application with Docker. You can modify this application to build more complex Flask applications or experiment with Docker Compose to

GitHub Link: https://github.com/Safiakhatoon767/flask-app

Youtube Link: https://youtu.be/AyNSo5IXb9s

Thank you for reading this blog๐Ÿ™

I hope it helps ๐Ÿ’•

โ€” Safia Khatoon

Happy Learning ๐Ÿ˜Š

ย