How to Containerize Python Flask Application

Shiny Hettiarachchi
2 min readNov 23, 2021

Docker is an open-source platform for building, deploying, and managing containerized applications.
Docker is supposed to enable standardized executable components combining application source code with the operating system libraries and dependencies required to run that code in any environment.
Docker is crucial a toolkit that enables developers to build, deploy, run, update, and stop containers using simple commands and work-saving automation through a single API.
Container technology offers application isolation, cost-effective scalability, and disposability, Lighter weight, Greater resource efficiency, Improved developer productivity.
Flask is a micro web framework written in python.
Before start building images, ensure that we have enabled Build Kit on our machine. Build Kit allows to build Docker images efficiently.
Then create a docker file for python on your directory.

FROM python:3.8

WORKDIR /app

COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt

COPY . .

CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0"]

Add docker build -t python-docker image name:latest . command on the application directory command prompt.

docker build -t python-imagename:latest .

Most of the people have problem with the key word “latest”. The ‘latest’ tag does not actually mean latest, it doesn’t mean anything. People are expected that the key word latest points out most latest image will be built with this command.

It’s just only a tag which applied to a docker image by default that do not have a tag.
The following those two commands will both results in a new image that being created and tagged as latest:

docker build -t python/image name .docker build -t python/image name:latest .

Latest tag is just a default value.

You can check the docker images that have already been created using the following command.

docker images

You can run the docker images by using the docker run — net host python-image name.

docker run --net host python-image name

You can check the running docker images by using the docker ps command.

docker ps

By confirming the docker image has successfully run, you can save the docker image by using the following command.

docker save python-image name

Reference

--

--