In this article we will look at the step-by-step guide to deploy a chatbot application on Google Cloud Platform (GCP) using Git and Docker. The application used as an example is a Python-based web chatbot with a Streamlit user interface.

Step 1: Containerize Your Application with Docker

Containerising your application using Docker before deploying it on cloud offers several benefits:

Portability — Docker packages the application and all its dependencies into a single unit which can be run on any device, avoiding machine-specific issues.

Isolation and Security — Containerization isolates the application from other containers and the host system, protecting against security breaches and dependency conflicts.

Scalability — Containers make it easy to scale applications as required. With orchestration platforms like Kubernetes, scaling can also be automated.

Consistency across all devices — Packaging all dependencies in the container helps it run smoothly on any device, crucial when collaborating across development, testing, staging, and production environments.

Management and Automation — Docker images can be versioned, shared, and rolled back quickly, supporting modern DevOps practices and automated testing.

Install Docker

Go to the Docker website and download Docker Desktop for Windows/macOS. For Linux, install Docker using the terminal with the appropriate apt-get commands.

Create a Dockerfile

In your project directory, create a file named Dockerfile and add instructions to build your Docker image. For a Python Streamlit app, the Dockerfile would use python:3.9-slim as the base image, set the working directory, copy and install requirements, expose port 8080, and run the Streamlit app with CMD ["streamlit", "run", "app.py", "--server.port=8080"].

Build the Docker Image

Run the following command in your terminal: docker build -t myapp .

The -t myapp flag tags/names the image, and the dot tells Docker to look for the Dockerfile in the current directory. Docker reads the instructions and builds an image that can then be used to create containers.

Step 2: Deploy on Google Cloud Using Free Credits

Create a Google Cloud Account

Go to cloud.google.com and create an account if you haven't already. You'll receive $200 in free credits valid for 90 days. Ensure billing is enabled for your project to use free credits.

Install Google Cloud CLI

Follow the instructions on the Google Cloud website to install the gcloud CLI. Open a terminal and run gcloud init to initialize the CLI and authenticate with your Google account.

Create a New Project

Log in to the Google Cloud Console. Click on the Select a project dropdown and click New Project. Enter a project name and click Create.

Enable Artifact Registry API

Search for "Artifact Registry" in the console and enable it. Navigate to APIs & Services > Dashboard, search for "Artifact Registry API" and click Enable.

Tag and Push Your Docker Image

Tag your Docker image with the Artifact Registry URL:

docker tag myapp us-central1-docker.pkg.dev/[PROJECT_ID]/[REPOSITORY_NAME]/my-chatbot

Then push the image to Artifact Registry:

docker push us-central1-docker.pkg.dev/[PROJECT_ID]/[REPOSITORY_NAME]/myapp

Deploy to Cloud Run

Use the following command to deploy your image to Cloud Run:

gcloud run deploy myapp-service --image us-central1-docker.pkg.dev/[PROJECT_ID]/[REPOSITORY_NAME]/myapp --region us-central1 --allow-unauthenticated

Replace [PROJECT_ID], [REPOSITORY_NAME], and service name with your actual values. Once the deployment is complete, you will see the URL of your deployed service.

Conclusion

Congratulations! You've successfully navigated the entire process of transforming your chatbot application from a local development environment to a production-ready deployment on Google Cloud Platform. By following this step-by-step approach, you've learned the technical aspects of containerization and experienced firsthand why Docker has become such an essential tool in modern application deployment.

The combination of Docker's containerization power and Google Cloud Platform's robust infrastructure gives your application the capability to scale and serve users reliably in production.