Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 74

Deploying Your Chatbot with Containers

Recommended resources

Welcome back! In the previous lesson, you successfully integrated your Streamlit front-end with the FastAPI backend, creating a fully functional AI Tutor that runs locally. You now have a decoupled system where the user interface communicates with the RAG pipeline via an API. This is a significant milestone and a professional software architecture pattern.

This lesson marks the final, exciting step for your application's development: deployment. We will take your local project and make it accessible to the world. To do this, we'll containerize it using Docker and deploy it to a cloud platform. Your experience with Docker, Kubernetes, and AWS will be a great asset here, allowing us to move quickly through the core concepts and focus on the practical implementation for this specific type of AI application.

Our goal is to package your chatbot into a portable, reproducible container and deploy it, transforming it from a local script into a live, shareable portfolio piece.

1. The "It Works on My Machine" Problem

Before we write any code, let's solidify the "why" behind this process. You've built an application that works perfectly on your computer. But if you were to share the code with a colleague or try to run it on a cloud server, you'd likely spend hours debugging dependency conflicts, Python version mismatches, and missing system libraries. This is the classic "it works on my machine" problem.

Docker solves this by packaging your application, along with all its dependencies—code, libraries, system tools, and runtime—into a single, isolated unit called a container. This container can run consistently on any machine that has Docker installed.

The video below provides an excellent explanation of this problem and introduces the key Docker concepts we'll be using.

How to Deploy AI Apps with Streamlit, Docker, and Hugging Face

This video, "How to Deploy AI Apps with Streamlit, Docker, and Hugging Face," by Shaw Talebi, clearly explains the motivation for using Docker in AI projects.

Please watch from the beginning to the end of the Docker explanation. Focus on: The value of deploying projects to generate value and build a portfolio. The "runs on my computer" problem and how Docker provides a solution. The definitions of a Dockerfile (the recipe), an Image (the snapshot), and a Container (the running instance).


2. Crafting Your Application's Dockerfile

To build a Docker image, we need a Dockerfile—a text file containing step-by-step instructions. While your local setup uses two separate services (Streamlit and FastAPI), many modern, free deployment platforms like Hugging Face Spaces are optimized for single-container applications.

For this deployment, we'll make a pragmatic choice: we will create a single Docker container that runs the entire application. This means the Streamlit script will directly import and call the RAG pipeline functions, similar to the monolithic approach we discussed. This simplifies our deployment process significantly while still leveraging the power of containerization.

Your project structure for deployment will be:

/ai_tutor_project
|-- app.py             # Streamlit UI + RAG logic combined
|-- requirements.txt   # All dependencies for UI and backend
|-- Dockerfile         # Our new Docker instructions
|-- vector_db/         # Your ChromaDB data
|-- .env               # Your API keys (will not be in the container)
|-- .gitignore         # To exclude .env, etc.

Let's create the Dockerfile. The official Streamlit documentation provides a robust template.

Deploy Streamlit using Docker - Streamlit Docs

The official Streamlit documentation offers a clear, line-by-line guide to creating a Dockerfile for a Streamlit app.

Read the sections titled "Create a Dockerfile" and "Dockerfile walkthrough". This will give you a detailed breakdown of each instruction we will use, from selecting a base image (FROM) to defining the startup command (ENTRYPOINT).

Based on that guide, here is a Dockerfile tailored for your AI Tutor project. Create a file named Dockerfile in your project's root directory and add the following content:




# 1. Use an official Python runtime as a parent image
FROM python:3.9-slim




# 2. Set the working directory in the container
WORKDIR /app




# 3. Copy the dependencies file to the working directory
COPY requirements.txt .




# 4. Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt




# 5. Copy the rest of the application code to the working directory
COPY . .




# 6. Expose the port Streamlit runs on
EXPOSE 8501




# 7. Add a health check to ensure the app is running
HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health




# 8. Define the command to run your app
ENTRYPOINT ["streamlit", "run", "app.py", "--server.port=8501", "--server.address=0.0.0.0"]

This file provides a complete, reproducible recipe for building your application's environment from scratch.


3. Building and Testing the Container Locally

Before deploying, we must verify that the container works on your local machine.

  1. Build the Image:
    Open a terminal in your project's root directory and run the docker build command. We'll tag (-t) our image as ai-tutor for easy reference.

    docker build -t ai-tutor .
    

    This command executes the steps in your Dockerfile, downloading the base image, installing dependencies, and copying your code. It might take a few minutes the first time.

  2. Run the Container:
    Now, run the image you just built. The -p flag maps your local machine's port 8501 to the container's port 8501. The --env-file .env flag is crucial; it securely passes the API keys from your local .env file into the container as environment variables.

    docker run -p 8501:8501 --env-file .env ai-tutor
    

You should see output from Streamlit, and your application will be available at http://localhost:8501.

This image shows the expected terminal output after successfully running a Docker container for a Streamlit app. Note the URL where the app is accessible.

Test the app thoroughly. If it works correctly inside the local container, you can be highly confident it will work anywhere.


4. Liftoff! Deploying to Hugging Face Spaces

With a working Docker image, you are ready to deploy. We will use Hugging Face Spaces, a free platform designed to host ML applications. It integrates seamlessly with Docker.

The video from earlier also contains a fantastic, step-by-step walkthrough of this exact process.

How to Deploy AI Apps with Streamlit, Docker, and Hugging Face

The "How to Deploy AI Apps" video continues with a practical guide to deploying a Dockerized application on Hugging Face Spaces.

Watch the final sections of the video, from Deploying the App and Managing Secrets. Follow along as the presenter: Creates a new Space and selects the Docker SDK. Connects the local Git repository to the Hugging Face remote. Pushes the code (including the Dockerfile) to the Space. Most importantly, adds API keys and other environment variables as "Secrets" in the Space's settings. This is the secure way to manage credentials in production.

By following these steps, you will trigger an automated build process on Hugging Face. The platform will use your Dockerfile to build the image and start the container, making your AI Tutor live on the internet.


Your Task: Launch the AI Tutor

It's time to bring everything together and deploy your capstone project.

  1. Consolidate Your Code:

    • If you haven't already, modify your app.py to directly import and call the RAG pipeline functions. The decoupled API was a great exercise, but for this deployment, a single script is simpler.
    • Create a single requirements.txt file that includes all packages for both the Streamlit UI and the RAG backend (e.g., streamlit, fastapi, uvicorn, langchain, openai, chromadb, etc.).
  2. Create and Test the Docker Image:

    • Create the Dockerfile as shown in Section 2.
    • Build your Docker image locally using docker build -t ai-tutor ..
    • Run and test your container locally using docker run -p 8501:8501 --env-file .env ai-tutor. Ensure the chatbot is fully functional.
  3. Prepare for Deployment:

    • Make sure your project is a Git repository (git init if needed).
    • Create a .gitignore file and add .env, __pycache__/, and other unnecessary files to it.
    • Commit all your project files (app.py, Dockerfile, requirements.txt, vector_db/ directory, etc.).
  4. Deploy to Hugging Face Spaces:

    • Go to huggingface.co/new-space.
    • Give your Space a name, select "Docker" as the Space SDK, and choose the free "CPU basic" hardware.
    • Follow the instructions to add the Space's Git repository as a remote for your local project (e.g., git remote add hf ...).
    • In your Space's "Settings" tab, go to "Variables and secrets" and add your OPENAI_API_KEY (and any other keys) as a secret.
    • Push your code to the new remote: git push hf main.
    • Watch the "Building" logs in your Hugging Face Space. Once it's "Running," your app is live!
  5. Share Your Work:

    • Grab the public URL of your live application (e.g., https://username-spacename.hf.space).
    • Add this URL to your project's README.md file on GitHub.

Conclusion

Congratulations! You have successfully containerized and deployed a sophisticated, end-to-end AI application. This is a critical skill that sets apart academic projects from portfolio-worthy, production-ready work. You've demonstrated your ability to not only build complex ML systems but also to package and share them in a robust and professional manner.

Key Takeaways:

  • Portability with Docker: Docker solves the "it works on my machine" problem by packaging your app and its entire environment into a portable container.
  • Deployment Pragmatism: Choosing the right architecture (monolithic vs. microservices) often depends on the deployment target. For platforms like Hugging Face Spaces, a single-container app is often the most direct path.
  • Secure Credential Management: Never hard-code API keys. Always use environment variables and the secret management features of your deployment platform.

Preview of the Next Lesson:

Your AI Tutor is now live, but a great project is more than just working code. In our next lesson, "Document the Project Architecture," we will focus on creating a high-quality README.md file. You will learn how to clearly explain your project's purpose, architecture, and functionality, ensuring that anyone who visits your GitHub repository—from recruiters to fellow developers—can immediately understand the scope and quality of your work.