How to Dockerize a Flask Application

Create a Basic Flask Application

Before you can create a Docker image with your application, you have to have a basic app that you can run locally or on a server.

In this section, you will create a basic app with Flask and then run it in a Docker container. You can use your preferred editor to create the app, or you can use the following command in your terminal to create a new app:

  1. Let’s begin with creating a new directory:

    mkdir flask-app && flask-app

  2. Next, create the Python virtual environment and then activate the environment.

    python3 -m venv venv source venv/bin/activate

  3. Now install the Flask python module under the virtual environment.

    pip install Flask

  4. The below command will create the requirements.txt file with the installed packages under the current environment. This file is useful for installing modules at deployments.

    pip freeze > requirements.txt

  5. Now, create a sample Flask application.. You can write your code in a .py file and run it with the python command.

    vim app.py

    Add the below snippt.

     123456789101112# Import flask modulefrom flask import Flask app = Flask(__name__) @app.route('/')def index():    return 'Hello to Flask!' # main driver functionif __name__ == "__main__":    app.run()
    
  6. Your sample Flask application is ready. You can run this script with Python now.

    flask run --host 0.0.0.0 --port 5000

    Running flask application at the command line

Running Flask App via CLI

Now that you have a basic app, you can run it in a Docker container by creating a file called Dockerfile in the same directory as your app.py file.

Create a Dockerfile for Your Flask Application

A Dockerfile is a file that contains instructions on how to build your image. It describes the entire process of building your image, from the installation of your Python app to the creation of your container.

  1. Let’s create a file named Dockerfile under the project directory. This is the file docker reads instructions to build an image.

    vim Dockerfile

    Add the following code:

    FROM python:3-alpine # Create app directory WORKDIR /app # Install app dependencies COPY requirements.txt ./ RUN pip install -r requirements.txt # Bundle app source COPY . . EXPOSE 5000 CMD [ "flask", "run","--host","0.0.0.0","--port","5000"]

    Save the file and close it.

  2. Next, create the Docker image by running the below-mentioned command. Here “flask-app” is the image name.

    docker build -t flask-app .

  3. This image will be created in local image registry. Then you can create a Docker container with the following command.

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

  4. Now, verify that container is running on your system.

    docker containers ls

  5. Finally, open a browser and connect to localhost on port 5000 (or use your own defined port). You should see the flask application.

Dockrise Flask Application

Dockrise Flask Application

A Dockerfile can be used to automatically create and update your image. If you're working in a team and different people are contributing code to the same application, this is useful.

Conclusion

You discovered how to develop a fundamental Flask app and a Docker image in this tutorial. Also, you learned how to generate and maintain your image automatically as well as how to setup a private registry. This is a fantastic method for producing reproducible builds and container images that are simple to distribute. If you need to set up servers or execute your software on a different machine, this can be useful.