Docker Toolkit: Dockerizing Flask App

Saurabhk
3 min readMay 3, 2021

Here’s a quick guide to Data Scientists for dockerizing your python app

Image by Author (BIG Fish Aquarium, Singapore)

As a ML Engineer or Data Scientist, you often train various ML Models. In most cases, you’d want to share it across teams to try it out on their device or maybe just want to deploy it on managed cloud services for demo. In such cases, Docker helps Containerizing all the code, packages, dependencies together allowing to ship across various environment easily.

In python there are plenty of frameworks that quickly lets you build an app around the trained ML model; rather it being limited to the JUPYTER notebook itself.

To start with, first we’ll look at how to go about Dockerizing python flask application

Install Docker

Install docker by typing out following commands on the ubuntu system

$ sudo apt install docker.io 
$ docker — version
$ sudo systemctl enable — now docker

Application File Structure

Observe the folder structure, You can keep/follow this file structure for almost any other python framework you work on.
Example: streamlit, FastAPI etc.

File Structure (tree -L 2)
  1. Here the app folder holds all the core Flask specific code
  2. dockerignore holds the files paths that we do not wish to include iin the final docker Image (I have mentioned path of my local venv folder).
  3. requirerments.txt files consist of all the packages & dependency required to run our flask app (so activate the local venv environment and run below command; this would list down all packages & dependency needed for the application to run)
pip freeze > requirements.txt

4. Now lets look at dockerfile below which eventually create a new image.

Docker File for Flask Application

Following are the explanations of the of each of the above commands in th above Dockerfile.

  1. Every Dockerfile has to begin with a FROM. FROM must be followed by an already existing image (either locally on your machine or from the DockerHub repository). I choose python:3.7.3-slim as my base image (slim tag refers to lite version of python).
  2. ENV command sets environment variable APP_HOME to /flask-app directory.
  3. WORKDIR is sets to APP_HOME which means the rest of the commands in the image will be executed from /flask-app working directory path.
  4. COPY command 1st copies only requiremet.txt file(This step is important as it save lot of time during rebuilds incase of minor updates to the code).
  5. RUN command ensures that pip is upgraded in the image
  6. RUN command installs all the packages mentioned in requirements.txt.
  7. RUN command displays folder structure in the image.
  8. COPY command copies all of the files from current from local directory ./app folder to the ./app folder in the images APP_HOME .
  9. Again WORKDIR is set to tthe newely copied ./app folder (as it holds flask executable app.py file)
  10. Again the RUN command to check if all files from local directory are copied to the Image successfully.
  11. CMD specifies what command to run within the container as it starts. Here, I run Flask app using gunicorn which ensures that the app launches immediately as the container has spun.

Docker Build

To Build the docker image, run below given command with sudo privilege's

$ sudo docker build -t app-flask:1.0 .
$ sudo docker run -p 5000:5000 app-flask
  1. The 1st command tags the image with app-flask
  2. Flask by default runs on 5000 port. In the 2nd command we do port-forwarding so as we are able to view the app in our browser.

And there you go, just open up your browser and launch your app!

$ sudo docker ps

Run the above command, to see more details associated with the image.

Further we can push this image to Docker Registry from where you team can do a quick docker pull. Some of the available option are Docker Hub, AWS ECR, Github registry.

--

--

Saurabhk

Data Science Enthusiast. Love Applied Research.