Running and Hosting Flask in a Docker Container: A Step-by-Step Guide

Posted by


Flask is a lightweight and flexible web framework for Python that makes it easy to build web applications. Docker is a platform that allows you to package your application and its dependencies into a container, which can then be run on any machine that has Docker installed. Combining Flask with Docker is a great way to simplify the deployment process of your web application.

In this tutorial, I will show you how to run and host a Flask application in a Docker container.

Step 1: Set up your Flask application
First, you need to create a Flask application. If you don’t already have one, you can create a simple Flask application by following these steps:

  1. Create a new directory for your Flask application and navigate into it.
  2. Create a new Python file, for example app.py, and add the following code to create a simple Flask application:
from flask import Flask

app = Flask(__name__)

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

if __name__ == '__main__':
    app.run()
  1. Save the file and install Flask by running the following command in your terminal:
pip install Flask

Step 2: Create a Dockerfile
Next, you need to create a Dockerfile to define the image for your Flask application. Create a new file named Dockerfile in the same directory as your Flask application and add the following code:

FROM python:3.8-slim

WORKDIR /app

COPY . .

RUN pip install -r requirements.txt

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

In this Dockerfile, we are using the official Python 3.8 slim image as the base image, setting the working directory to /app, copying all files from the current directory to the /app directory in the container, installing the dependencies listed in requirements.txt, and finally running the app.py file as the entrypoint.

Step 3: Create a requirements.txt file
Create a new file named requirements.txt in the same directory as your Flask application and add the following line:

Flask

This file lists all the dependencies required for your application. Make sure to add any additional dependencies your application may require.

Step 4: Build the Docker image
Now that you have created your Dockerfile and requirements.txt file, you can build the Docker image for your Flask application by running the following command in your terminal:

docker build -t flask-docker .

This command will build the Docker image using the Dockerfile in the current directory and tag it with the name flask-docker.

Step 5: Run the Docker container
Once the Docker image is built, you can run the Docker container using the following command:

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

This command will run the Docker container in detached mode -d, map port 5000 in the container to port 5000 on the host machine -p 5000:5000, and use the flask-docker image.

Step 6: Access your Flask application
You can now access your Flask application by opening a web browser and navigating to http://localhost:5000. You should see the message "Hello, World!" displayed, which is the response from the / route defined in your Flask application.

Congratulations! You have successfully run and hosted your Flask application in a Docker container. This setup makes it easy to deploy your Flask application to any machine that has Docker installed, simplifying the deployment process and ensuring consistency across different environments.

0 0 votes
Article Rating
14 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@veal225
1 month ago

Simple and clean. Thank you!

@KH-kg4bm
1 month ago

the volume is so quiet even with an external speaker

@LarsGölz
1 month ago

Incase you run into some errors with flask you can try using

RUN pip3 install Flask –break-system-packages

worked for me 🙂

@DarshanSingh-iq7gq
1 month ago

my docker container exits as soon as its run. I cannot see localhost:5000

@tasmaniandaemon
1 month ago

my friend do not make videos

@SosaG21
1 month ago

I owe you a comment and a thank you for this video! Been looking for something like this for a week. Just what I needed

@jlrodricar
1 month ago

Great video, very helpful!! 🐍🚤

@djohnworthy1040
1 month ago

When I use docker app. I cant port 5000:5000 it does not let me. When I change it i have no issues. I have problem when I put my docker into aws. Task definition starts and then stops

@2sourcerer
1 month ago

From the Flask Doc: "When running publicly rather than in development, you should not use the built-in development server (flask run). The development server is provided by Werkzeug for convenience, but is not designed to be particularly efficient, stable, or secure." So only use this method for development.

@SiphepheloMlungwana
1 month ago

Thank you for this tutorial.

@jackq9197
1 month ago

am following this tutorial in 2023 using vs code running on mbp m1. I faced a flask is having an error "import flask could not be resolved Pylance(—-). Any idea how to solve this issue?

@abilashp6367
1 month ago

please explain properly.

@din0toxic
1 month ago

Very helpful tutorial, Thank you very much <3

@nthapostle8070
1 month ago

In the last line where we have
CMD ["python3", "-m", "flask", "run", "–host=0.0.0.0"]

could we use
RUN python3 -m flask run –host="0.0.0.0"?

just to be consistent with the format of the earlier commands