How to Dockerize your fast api app

Python provides many ways to distribute your python projects. One such way is by using an important technology called Docker. Docker is an open-source application that allows administrators to create, manage, deploy, and replicate applications using containers. It is basically a platform that enables developers to make their applications portable by putting them inside a container. You can make your python project portable using it. Docker container removes dependency issues by isolating your project with system. Docker image created of your project can be ported anywhere. Official Documentation of Docker. In this article we will see an example in which we will be converting our Fast api app into docker image and see some basic commands of docker along with it. Fast is a micro-framework for building small web applications. We are just using it as an example of Python project. One can use any other python project in the same manner. For more information on Fast API, visit here.


In the last blog i have explanied about how to create a FAST API for ML Models
Have a look at here

Make sure docker is installed in your environent.

Setup up Dockerfile

Create a Docker file named Dockerfile in that add these lines

FROM python:3.7-buster
COPY . /app
WORKDIR /app
RUN apt-get update -y
RUN apt-get install virtualenv python3-pip -y
RUN pip install --upgrade pip
RUN pip install -r requirement.txt
EXPOSE 8000
CMD python app.py
Let’s see what our Dockerfile does. FROM python:3.7-buster pulls python 3.7’s image from the docker hub, COPY command copies the fast api app into the container and WORKDIR command sets the working directory. RUN apt-getget update -y ,makes the environment up to date and the next command "RUN apt-get install virtualenv python3-pip -y"we are creating a virtual environment to run this appthe next command "RUN pip install --upgrade pip" helps to upgrade the package manager to the latest version. “RUN pip install -r requirements.txt” this command will install each requirement written in “requirements.txt ” file one by one bye on the host system. EXPOSE as the name says exposes port 8000 which Fast api app will use to the container so that later it can be mapped with the system’s port. Entrypoint and CMD together just execute the command “python app.py” which runs this file.

Create a requirement.txt file based on your dependency
i have updated the Github based on the dependency do check


Build a Docker Image

Make sure you are in root directory of the project and run the following command
sudo docker build --tag fast-api-demo-boston .

The above command will create an app with the tag fast-api-demo-boston.
Note: Enter the password if required.


Run the docker image we just created.

Run the following command
sudo docker run --name fast-api-demo-boston -p 8000:8000 fast-api-demo-boston





In the above command, -name parameter gives name to the container and -p parameter maps the host’s(my laptop in this case) port 8000 to the container’s port 8000 since the container is isolated and we need to map it in order to access it from external environment. And at last “fast-api-demo-boston” refers to the image to run.


since i have already created the docker image i need to start and run the container so the image
is little bit different.The above command has to give output like above.


Close the image by running “docker stop” command. To know the container id enter “docker ps” command.
It shows all the running containers. Visit Docker’s website for more docker commands like “saving the image as tar file” and
exporting it in another system.


All the code are availabe in my github account do check and leave your feedback

Post a Comment

Previous Post Next Post