A Docker application developer will tell you that Docker is an open-source software that allows administrators to create, manage and deploy containers. You can make your Python project portable with docker by placing it inside a container.
Let’s expand our knowledge about Docker and build a foundation for containers. A previous blog on Docker can provide more information about containers. Click here to learn more.
We will now discuss how to Dockerize Flask Applications. Flask is a Python web development framework. Two popular web development frameworks in Python are Django and Flask. Django is heavier than Flask because it has many dependencies. We will now show you how to deploy a Flask Application using Docker.
Prerequisites:
1. Install Python on your computer.
For installing Python on Windows, visit this URL https://www.python.org/downloads/ and download the latest Python version.
The following command is applicable to Ubuntu machines.
sudo apt update -ysudo apt-get install python3-pip -y12sudo apt update -ysudo apt-get install python3-pip -y2. Install Flask library. To install Flask, you can use the following command.
pip1pip1pip2pip3pip4pip
Guide to setting up Flask Application
You will now need to create a Python file named app.py in a folder. Copy the following code into that file.
from flask import Flaskapp = Flask(__name__)@app.route(‘/’)def index(): return “
This is just a basic Flask Application
“if __name__ == “__main__”: app.run(host =’0.0.0.0′, port = 8080, debug = True)123456789from flask import Flaskapp = Flask(__name__)@app.route(‘/’)def index():return “
This is just a basic Flask Application
“if __name__ == “__main__”:app.run(host =’0.0.0.0′, port = 8080, debug = True)This a simple Flask Application to print some message on the browser. It will create a server, and run it on port 8080. You can choose any port you prefer. Save the file and then run python application.py from the command prompt.
You’ll notice that a development server is started.
Now, when you browse this URL: http://localhost:8080, the content that was written inside the return keyword will get displayed there.
This means that your Flask Application works perfectly.
Next, create a file named requirements.txt. Then, add a flask to that file. This is to install the flask libraries.
Create a Dockerfile named Dockerfile. Make sure you don’t add any extensions to the file name. Copy the following command into that.
FROM python.alpine3.7COPY /myappWORKDIR /myappRUN pip install -r requirements.txtEXPOSE 8080ENTRYPOINT [ “python” ]CMD [ “app.py” ]1234567FROM python:alpine3.7COPY . /myappWORKDIR /myappRUN pip install -r requirements.txtEXPOSE 8080ENTRYPOINT [ “python” ]CMD [ “app.py” ]
The FROM keyword is used for downloading the base image. It is of Python and alpine3.7 is its variant. Next, we will copy all files in the current directory to my app. This is done inside the container. WORKDIR is used for defining the working directory. If that keyword is not used, it will treat root the working directory. RUN is used for running the command we wish to execute. pip install-r requirements.txt will execute all of the modules found in the file. EXPOSE will expose port 8080. We have already mentioned that the development server should be running at port 8080 in the code. We need to expose this port. These lines allow you to run the application automatically after you create a container.
This is what Dockerfile commands look like.
Next, you will need to install Docker onto your machine. Or, you can