Dockerize your Flask App

Python provides many ways to distribute your python projects. One such way is by using an important technology called Docker. Docker is an open-source application that allows administrators to create, manage, deploy, and replicate applications using containers. It is basically a platform that enables developers to make their applications portable by putting them inside a container. You can make your python project portable using it. Docker container removes dependency issues by isolating your project with system. Docker image created of your project can be ported anywhere. Official Documentation of Docker. In this article we will see an example in which we will be converting our Flask app into docker image and see some basic commands of docker along with it. Flask is a micro-framework for building small web applications. We are just using it as an example of Python project. One can use any other python project in the same manner. For more information on Flask, visit here.

Setup Flask and Dockerfile

  • Make Project Folder

    Open your terminal and make a folder for your flask application let’s say “flask_docker_demo”
    by executing the following commands:

    $mkdir flask_docker_demo
    $cd flask_docker_demo #to change the directory

    You can also create the folder and above files manually. If you don’t have Gedit installed then you can use any code editor.
    Enter following in terminal



    $gedit demo.py

    Paste the following code into “demo.py”.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    from flask import Flask
    app = Flask(__name__)
      
    @app.route('/')
    def hello():
        return "welcome to the flask tutorials"
      
      
    if __name__ == "__main__":
        app.run(host ='0.0.0.0', port = 5001, debug = True

    chevron_right

    
    

  • Insert the following code into the Dockerfile created earlier

    Add a new file and name is as “Dockerfile” if you haven’t created it already using gedit. Don’t give any extension.
    Paste the following code into it

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    FROM python:alpine3.7
    COPY . /app
    WORKDIR /app
    RUN pip install -r requirements.txt
    EXPOSE 5001
    ENTRYPOINT [ "python" ]
    CMD [ "demo.py" ]

    chevron_right

    
    

    Let’s see what our Dockerfile does. FROM python:alpine3.7 pulls python 3.7’s image from the docker hub, COPY command copies the flask app into the container and WORKDIR command sets the working directory. “RUN pip install -r requirements.txt” this command will install each requirement written in “requirements.txt ” file one by one bye on the host system. EXPOSE as the name says exposes port 5001 which Flask app will use to the container so that later it can be mapped with the system’s port. Entrypoint and CMD together just execute the command “python demo.py” which runs this file.

  • Copy the following into “requirements.txt” file

    Create the requirements.txt manually if you haven’t created it already with gedit and Add the following line into it

    flask
  • We should have the following structure right now.

  • Test the flask app

    Go inside the root folder “flask-docker-demo” if you aren’t already in this directory and run the following command “python demo.py”
    It should start our development server which comes with the flask on “http://0.0.0.0:5001/”.
    see the screenshot below.

    We should have the following output in the browser

  • Close the server by pressing CTRL + C

    Refresh the browser again and it should give an error since the server has been closed.

Creating a docker image of the project and How to run it

  • Build the Docker image

    Make sure you are in root directory of the project and run the following command.

    sudo docker build --tag flask-docker-demo-app .

    The above command will create an app with the tag flask-docker-demo-app.
    Note: Enter the password if required.

    Refer screenshot below.

  • Run the docker image we just created.

    Run the following command:

    sudo docker run --name flask-docker-demo-app -p 5001:5001 flask-docker-demo-app


    In the above command, -name parameter gives name to the container and -p parameter maps the host’s(my laptop in this case) port 5001 to the container’s port 5001 since the container is isolated and we need to map it in order to access it from external environment. And at last “flask-docker-demo-app” refers to the image to run.

  • Test again .

    Refresh the browser

  • Close the image by running “docker stop” command. To know the container id enter “docker ps” command. It shows all the running containers. Visit Docker’s website for more docker commands like “saving the image as tar file” and exporting it in another system.


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.