Build and deploy a flask application inside docker

DockerOperating SystemOpen Source

Docker allows you to build, manage and deploy applications inside containers. It provides a packed environment and allows developers to make portable applications by containerizing them. You can easily build a flask application, manage it and make it portable all using a single technology, docker. You can use similar techniques to build and deploy other python frameworks as well.

In this article, we will be discussing how to build a simple application using flask and convert that into a docker image by containerizing it. You can follow the steps mentioned below to do the same.

Steps

Create a new project folder. Let’s name it flask_project.

Move to that directory and create a python file. Let’s call it app.py.

Write a simple flask code inside the file. Check out the code below.

#Import the flask module
from flask import import Flask

#Create a Flask constructor. It takes name of the current module as the argument
app = Flask(__name__)

#Create a route decorator to tell the application, which URL should be called for the #described function and define the function

@app.route('/')
def tutorialspoint():
return “Welcome to TutorialsPoint”

#Create the main driver function
if __name__ == ‘__main__’:
#call the run method
app.run()

You can copy the above code inside the app.py file which displays a simple welcome statement.

Create another file called requirements.txt. This file would contain a list of packages to be installed. For this project, we only need Flask library to be installed. Hence, inside the file, only include “flask” (Without inverted commas).

Now, inside the root directory, create another file called dockerfile. This file would contain all the commands that would help you build the docker image.

Inside the dockerfile, copy the following code.

#Create a ubuntu base image with python 3 installed.
FROM python:3

#Set the working directory
WORKDIR /usr/src/app

#copy all the files
COPY . .

#Install the dependencies
RUN apt-get -y update
RUN pip3 install -r requirements.txt

#Expose the required port
EXPOSE 5001

#Run the command
CMD [“python3”, “./app.py”]

The dockerfile created above creates an ubuntu base image with python installed in it. It sets a working directory and then copies all the files from the host machine to the docker server. It then runs an update command and installs flask which is mentioned in the requirements file. It then exposes the port 5001 which would run our flask application and using CMD command, it runs the app.py file.

Now, build the image using the following command. Make sure you are in the root directory.

sudo docker build -t flaskproject . (don’t forget the dot)

After building the image, run the bash inside the docker container using an interactive shell through the following command.

sudo docker run -ti -p 5001:5001 flaskproject bash

The above command runs the image by connecting the ports and opens the bash.

Inside the bash, run the command -

python3 app.py

You will see something like this appearing on the screen.

  • Serving Flask app "app" (lazy loading)

  • Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.

  • Debug mode: on

  • Running on http://0.0.0.0:5001/ (Press CTRL+C to quit)

  • Restarting with stat

  • Debugger is active!

This means that your flask application is successfully running on the container can be accessed through port 5001 on your local machine.

Open a browser and go to the url -  http://0.0.0.0:5001/

You will see the following message - “Welcome to TutorialsPoint”

To conclude, using these steps you can easily build and deploy a flask application inside your docker container and can access the application by connecting the ports to your local machine.

raja
Published on 01-Oct-2020 19:30:05
Advertisements