Deploy a Tensorflow App using Flask and Docker

In this article, I will go through dockerizing a flask deep learning app. The project is divided into 2 parts. The first part consists of creating the TensorFlow app using Flask. You can find the first part here.

In the second part (this article), we will containerize this app using Docker.

Tensorflow app using docker

What is docker?

Docker is a software platform that enables rapid development, testing, and deployment of programs. Docker organizes software into standardized units called containers, which include everything the software requires to execute, such as libraries, system tools, code, and runtime.

First, we need to install docker if you have not already.

Next, open the docker desktop and check if you have the green section at the bottom which means Docker is working with no problem. You may encounter some problems running Docker at the beginning, so make sure to fix them before continuing this tutorial.

I will open the Vscode editor, and open the folder that contained my jupyter notebook file of “shoes classification”.

I will create a new file and I will name it “Dockerfile” without any extension.

I will write the following code:

FROM python:3.10.1 
ADD app.py .
COPY requirements.txt .
RUN pip install  --upgrade pip -r requirements.txt
COPY .  .
EXPOSE 5000
CMD [ "flask", "run", "--host=0.0.0.0","--port=5000" ]

I will create another file, I will name it “docker-compose.yml”, and I will write the following code:

services:
  application:
    container_name: application
    image: lamyauser/application
    ports:
      - "5000:5000"
    build:
      context: .
      dockerfile: Dockerfile

On the Vscode terminal, I will write the following command:

docker compose up --build.

We will wait until it finishes.

Now, we will push the image to the docker hub by typing the following command:

docker compose push

This step will take some time.

We can use “docker play” to run our app on the cloud.

What is play with docker?

Play With Docker provides the experience of having a free Alpine Linux Virtual Machine in the cloud where you can construct and operate Docker containers as well as clusters using Docker capabilities such as Swarm Mode.

Click on start.

Then click on “add new instance”.

I will type docker run -p 5000:5000 lamyauser/application.

you need to replace lamyauser with your docker hub username and application by the name of the app you wrote in the docker-comose.yml file.

Click on open port and write 5000 (the port number).

Now, you have your TensorFlow app running on the cloud, but this “docker” instance will be deleted after 4 hours.

If you faced any problems, don’t forget to write them down in the comment section.