Dockerize flask application

Dockerize a Basic Flask app

Dockerize any application isn’t Rocket Science you just have to understand the system-level dependencies of the application for this you have to understand to basic requirement of you current environment and just have to configure it over docker using Dockerfile.

Let we have already configure Flask project like basic-flask-app , so our hierarchy of file within the folder looks like this :

├── app.py
├── README.md
└── requirements.txt

Step 1 : Create a Dockerfile

“Dockerfile” is a Set on Instructions given to a base image and triggered by docker daemon line by line called as Step in During and that’s why image is created into multiple layered architecture. Don’t Panic just go through the command.

vi Dockerfile

Add the following line into the Dockerfile

FROM ubuntu:latest
RUN apt update
RUN apt install python3 python3-pip -y
COPY . /app
WORKDIR /app
RUN pip3 install -r requirements.txt
ENTRYPOINT ["python3"]
CMD ["app.py"]
EXPOSE 5000

Step 2 : Build Image using Dockerfile

After creating and saving the Dockerfile we can easily build the docker image using docker build command and tag the image with a name as given below :

docker build -t "basic-flask" .

Step 3 : Run “basic flask “Docker image

After Build we have to run the image to create a container so go through the step and Remember to assign port(9100) as given in the command below :

docker images  //Just-for-fun
docker run -it -p 9100:5000 basic-flask
If you saw this screen then everything works fine

Our/Your flask application is dockerized and In Up & Running State, so test it you can open http://localhost:9100 on your browser. Or

curl http://localhost:9100

Congratulations !!! You have Successfully Dockerize the Flask Application.

Just to Confirm!!! It is a Basic Flask app, which has no DB connected. to make it work with DB you can to configure the DB image and Use docker-compose to Run them both as a Container Stack.