Docker Container from Scratch to End for Flask Apps in Python
Docker With Python Flask App
- 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.
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()
2. 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
2. To install Docker, enter the below command on terminal:
$ sudo apt install docker.io
3. 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”]
2. 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
2. 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.
2. To check the compiled docker images use below command.
$ sudo docker images
3. 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.