Dockerize a Python application.

Dockerize a Python application.

ยท

4 min read

Dockerize a Python application - Intro

This article will show how to dockerize a simple Python application.

If you prefer a video version, you can find it here:

All the code is available in the video description: https://youtu.be/zGP_nYmZd9c

๐Ÿ’ก Prerequisites

  • Docker installed on your machine

  • A Python application (we will use one by cloning a repository)

๐Ÿ“ฅ Clone the GitHub repository

Clone the GitHub repository

git clone https://github.com/patrickloeber/ml-deployment.git

Now step into the ml-deployment/docker-flask/app directory

cd ml-deployment/docker-flask/app/api

Now open this directory in your favorite editor. If you are using VS Code, you can do this by typing

code .

โš ๏ธ Since the repository is already the final version, if you want to follow along, delete the 2 files in the app/api directory:

  • Dockerfile

  • docker-compose.yaml

An alternative is to leave them as they are and just keep reading :)

๐Ÿณ Docker

When you open the project, you should have something like this:

project structure

We will create:

  • a Dockerfile

  • a docker-compose.yml file

๐Ÿ‹ Dockerfile

Create a file called Dockerfile in the app directory. This file will contain the instructions to build the Docker image.


FROM python:3.8

COPY requirements.txt .

RUN pip install -r requirements.txt

RUN python -c "import nltk; nltk.download('omw-1.4'); nltk.download('wordnet')"

COPY . .

EXPOSE 5000

CMD ["flask", "run", "--host=0.0.0.0", "--port=5000"]

Explanation of the Dockerfile:

  • FROM python:3.8: Use the Python 3.8 image from Docker Hub

  • COPY requirements.txt .: Copy the requirements.txt file from the current directory to the Docker image

  • RUN pip install -r requirements.txt: Install the dependencies from the requirements.txt file

  • RUN python -c "import nltk; nltk.download('omw-1.4'); nltk.download('wordnet'): Download the WordNet corpus

  • COPY . .: Copy all the files from the current directory to the Docker image

  • EXPOSE 5000: Informs to use the port 5000

  • CMD ["flask", "run", "--host=0.0.0.0", "--port=5000"] Run the Flask application

๐Ÿ™ Docker Compose

Create a file called docker-compose.yml in the root directory. This file will contain the instructions to run the services (in this case just one).

version:  "3.7"

services:
  mlapp:
    container_name: mlapp
    image: francescoxx/mlapp
    ports:
      - "5000:5000"
    build:
      context: .
      dockerfile: Dockerfile

Explanation of the docker-compose.yml:

  • version: "3.7": Use the version 3.7 of the Docker Compose file format

  • services:: The services to run (in this case just one)

  • mlapp:: The name of the service

  • container_name: mlapp: The name of the container. It doesn't have to be the same as the service name but it's convenient.

  • image: francescoxx/mlapp: The name of the image to use. โš ๏ธ replace francescoxx with your dockerhub username or change the name here

  • ports:: The ports to expose

  • build:: The instructions to build the image: the context (the current directory) and the Dockerfile to use

๐Ÿƒโ€โ™‚๏ธ Build and run the application

To run the application, you can use the following command:

docker compose up

Note: in case you get an error and you want to run this command again, you can use docker compose up --build to rebuild the image.

If you see something like this, it means that the application is running:

Flask app running with docker compose

๐Ÿ” Test the application

This application is a simple Flask application that returns the sentiment of a text. Let's test if that works with Postman.

We will make 2 simple test with:

  • positive text ๐Ÿ‘

  • negative text ๐Ÿ‘Ž

Test 1

Make a POST request to http://localhost:5000/predict with the following body (be sure to have as a header Content-Type: application/json):

ImagPostman request

{
    "text": "May the force be with you"
}

You should get the following response:

Postman response

Test 2

Make a POST request to http://localhost:5000/predict with the following body (be sure to have as a header Content-Type: application/json):

{
    "text": "I hate you"
}

Postman request

This does work. You can also check the logs of the container to see what's happening:

docker compose logs

๐Ÿ’ฟ Push the image to Docker Hub

Last thing we can do is to push the image on Dockerhub. To do that, you need to create an account on Docker Hub. Then, you can push the image with the following command:

โš ๏ธ replace francescoxx in the docker-compose.yml file with your Docker Hub username!

docker compose push

This will take a while, but after that, you can check your Docker Hub account and you should see the image there:

Docker Hub image

๐Ÿ Conclusion

In this tutorial, we have seen how to:

  • create a Docker image for a Python application

  • run the application with Docker Compose

  • push the image on Docker Hub.

You can check the 2 videos below for a video version:

First video by Patrick Loeber!

That's all.

If you have any questions, drop a comment below.

Francesco

Did you find this article valuable?

Support Francesco Ciulla by becoming a sponsor. Any amount is appreciated!