Docker With Python Flask App
All around the globe the attraction of the Docker is increasing Day by Day. In the last few years, Docker has gained extrema popularity. But what is there as such so it is holding that much value. So, let's see how docker is more important as compared to VMs (the consent of the 1990s).
- Once Docker image is built on one environment then same can be deployed to QA or production which will save lots of QA and Prod issues.
- Docker containers are executed with the Docker engine rather than the hypervisor. Containers are therefore smaller than Virtual Machines and enable faster start-up with better performance, less isolation and greater compatibility possible due to sharing of the host’s kernel
- If the application is designed to provide scalability and high availability, then containers are the best choice else application can be placed in a virtual machine.
- Building Apps over docker container and then running anywhere either on any another local machine or on cloud is easy.
Lest see how to create the sample python Flask app, then create the Docker containers, how to build it and Run it after all will see how to push it to Azure Cloud in another blog.
A) First Create the sample Flask App in Python on Ubuntu Machine.
- Let's create a sample Python Flask Application as
$ sudo nano app.py
Paste the below code to this file and save the file.
from flask import Flask
app = Flask(__name__)
return "Hello World!"
if __name__ == '__main__':
- Then verify the app by running locally as below command.
$ sudo python app.py
Now the application should be open in the browser on default 5000 port at http://localhost:5000
After verification, we can press the CTRL+C to stop the running API.
B) Install the Docker on Ubuntu Machine
- Run the below command to Ubuntu terminal to update the local database of software to make sure you’ve got access to the latest revisions.
$ sudo apt-get update
- To install Docker, enter the below command on terminal:
$ sudo apt install docker.io
- To start the Docker on the start of the Machine execute below 2 commands in the terminal.
$ sudo systemctl start docker
$ sudo systemctl enable docker
C) Create the Docker File.
This is an important step in creating Docker containers.
- Create a Docker File without any extension with below command at the same location where python file is present.
$ sudo nano Dockerfile
Paste the below code to this docket file.
#Dockerfile — this is a comment. Delete me if you want.FROM python:3.7COPY . /app
ADD PossWordsDF.csv /var/
ADD requirements.txt /app/
RUN pip install -r requirements.txt
- Save the file.
D) Create a requirnment.txt file to deploy the packages depend.
- Create txt file with below command on the same location.
$ sudo nano requirnment.txt
- Add the below dependency package to this file and save the file.
Note: To add more dependency packages, just add each package to a new line.
E) Compile the docker image.
- In terminal go to the same location where all the above files are created and execute below command.
$ sudo docker build -t -d DockerContainerName:latest .
-d is for detached mode, we can use -it to interactive mode as well and . is the location of current folder.
- To check the compiled docker images use below command.
$ sudo docker images
- To run this Docker file run it using below command.
$ sudo docker run -p 5000:5000 DockerContainerName:latest
-p is for the port on which this docker container will run.
Now the app should be ready on the port 5000 on localhost.