Share via:

Docker Container from Scratch to End for Flask Apps in Python

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).

 

  1. 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.
  2. 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
  3. 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.
  4. 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.

  1. 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__)

@app.route(‘/’)

def hello():

return “Hello World!”

if __name__ == ‘__main__’:

app.run()

 

  1. 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

  1. 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

 

  1. To install Docker, enter the below command on terminal:

$ sudo apt install docker.io

 

  1. 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.

  1. 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/

WORKDIR /app

RUN pip install -r requirements.txt

ENTRYPOINT [“python”]

CMD [“app.py”]

  1. Save the file.

 

D) Create a requirnment.txt file to deploy the packages depend.

  1. Create txt file with below command on the same location.

$ sudo nano requirnment.txt

  1. 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.

flask

 

E) Compile the docker image.

  1. 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.

  1. To check the compiled docker images use below command.

$ sudo docker images

  1. 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.

Share via:
Omnichannel has been around for more than a decade...
Earlier in 2021, retailers could create an intelligent supply...
Forced outages – Cost implications Most industries follow best...