In this tutorial, you will learn how to create a basic Flask app with Docker. You will set up your app with a Dockerfile, and manage the images with an automated build process.

In this process, you’ll also learn how to use multiple Python virtual environments and keep your source code organized. If you’re new to Python or Flask, you may want to check out our beginner guide to Python as well as our beginner guide to Flask first. They cover the basics of these frameworks so that you can follow along better in this tutorial. Let’s get started!

What is Flask?

Flask is a lightweight Python framework for building web applications. It is simple, flexible, and pragmatic. It can be easily extended with the use of extensions and plug-ins. Flask can be used to create small websites or large-scale, complex applications.

Flask is used by companies like Pinterest, Twitter, Walmart, and Cisco. One of the common uses of Flask is for REST APIs that are used to interact with other systems. Applications written in Flask can also easily be deployed to a server or a cloud.

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 module 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.

    # Import flask module

    from flask import Flask

    app = Flask(__name__)

    @app.route(‘/’)

    def index():

        return ‘Hello to Flask!’

    # main driver function

    if __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 
    
    How To Create and Run a Flask Application Using Docker Docker Dockerfile Flask Python Python Framework
    Running flask application at command line

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 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  
    

    How To Create and Run a Flask Application Using Docker Docker Dockerfile Flask Python Python Framework

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

    How To Create and Run a Flask Application Using Docker Docker Dockerfile Flask Python Python Framework

You can use a Dockerfile to automate the building and updating of your image. This is helpful if you’re working with a team and people are adding code to the same application.

Conclusion

In this tutorial, you learned how to create a basic Flask app and build a Docker image with it. You also learned how to create a private registry and automate the building and updating of your image. This is a great way to create reproducible builds and container images that are easy to share and deploy. This can be helpful if you need to set up servers or if you want to run your app on different machines.