Deploy on IBM cloud an AI-powered web app using Docker

Dr. Marco Berta
3 min readMar 5, 2024

Docker is a powerful tool that allows developers to package their applications into containers, making it easier to deploy and run them in different environments. In this article, we will walk through the process of building a Docker image from a Python package named biketouradvisor, and then deploying a web application on IBM Cloud using that Docker image.

To start off, let’s assume that you already have a Python package named biketouradvisor that you want to containerize. This package uses the Langchain Python library to give cycling tour advice for a given city. The source code can be found in the GitHub repository

https://github.com/opsabarsec/langchain_demo.git

together with dependencies stored in the requirement.txt file.

Code explanations are given in my dedicated YouTube playlist

https://www.youtube.com/playlist?list=PLODNqjV73iNukBgQ1LAnFkO-Qco2RLkra

After you got the code, the procedure is that for any standard Python package.

The first step is to create a Dockerfile in the root directory of your project. This file will contain the instructions needed to build the Docker image.

Here is an example Dockerfile for building a Docker image from the package:

— — Dockerfile — —

# Use the official Python image as the base image

FROM python:3.11.4

# Set the working directory in the container

WORKDIR /app

# Copy the Python requirements file

COPY requirements.txt .

# Install the Python dependencies

RUN pip install -r requirements.txt

# Copy the Python package files

COPY biketouradvisor /app/biketouradvisor

# Set the default command to run when the container starts

CMD [“python”, “/app/biketouradvisor/langchain_ridetour.py”]

— —

In this Dockerfile, we are using the official Python image as the base image, setting the working directory to /app, installing the Python dependencies from a requirements.txt file, copying the biketouradvisor package files into the container, and setting the default command to run the langchain_ridetour.py file from the biketouradvisor package.

Once you have created the Dockerfile, you can build the Docker image by running the following command in the terminal:

docker build -t biketouradvisor .

This command will build the Docker image with the tag biketouradvisor.

Now that we have successfully built it, we can deploy a web application on IBM Cloud using this Docker image. To do this, we need to upload the Docker image to a container registry on IBM Cloud.

First, log in to IBM Cloud using the IBM Cloud CLI by running the following command:

ibmcloud login

Then, target the container registry service on IBM Cloud by running the following command:

ibmcloud cr region-set <region>

If you are registered as a student for IBM you can access for free the Code Engine which you can use from a VS_Code interface

Next, tag the Docker image with the IBM Cloud container registry URL by running the following command:

docker tag biketouradvisor <region>.icr.io/<namespace>/biketouradvisor

Finally, push the Docker image to the IBM Cloud container registry by running the following command:

docker push <region>.icr.io/<namespace>/biketouradvisor

Once the Docker image has been pushed to the IBM Cloud container registry, you can deploy a web application on IBM Cloud using this Docker image by creating a Kubernetes cluster, configuring a deployment, and exposing the deployment as a service.

To keep this article concise I will not dive into the details of building a flask API, Docker structure et.. But if you want to have your own AI app running on a cloud and being accessible to the world, Docker is a powerful tool that simplifies the process of packaging and deploying applications. By following the steps outlined in this article, you can easily build a Docker image from a Python package and deploy a web application for example on IBM Cloud.

--

--

Dr. Marco Berta

Senior Data Scientist @ ZF Wind Power, Ph.D. Materials Science in Manchester University