Dockerize your Flask App - GeeksforGeeks

29 Dec, 2019
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.
Make Project FolderOpen 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 directoryYou 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.pyPaste the following code into “demo.py”.from flask import Flaskapp = 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) 
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”.
from flask import Flaskapp = 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) 
Insert the following code into the Dockerfile created earlierAdd 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 itFROM python:alpine3.7COPY . /appWORKDIR /appRUN pip install -r requirements.txtEXPOSE 5001ENTRYPOINT [ "python" ]CMD [ "demo.py" ]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.
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
FROM python:alpine3.7COPY . /appWORKDIR /appRUN pip install -r requirements.txtEXPOSE 5001ENTRYPOINT [ "python" ]CMD [ "demo.py" ]
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” fileCreate the requirements.txt manually if you haven’t created it already with gedit and Add the following line into itflask
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 appGo 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
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 + CRefresh the browser again and it should give an error since the server has been closed.
Refresh the browser again and it should give an error since the server has been closed.
Build the Docker imageMake 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.
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-appIn 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.
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
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.
python-utility
Web technologies
Python
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python Dictionary
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Different ways to create Pandas Dataframe
Top 10 Front End Developer Skills That You Need in 2022
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?