🧱 Lesson 7.1: Hosting with Docker
🎯 Learning Objectives
By the end of this lesson, learners will:
-
Understand containerization and the purpose of Docker in app deployment.
-
Create a
Dockerfilefor their AI chatbot backend. -
Build and run containers locally.
-
Use Docker Compose for multi-service apps (e.g., backend + database).
-
Push Docker images to Docker Hub.
-
Deploy Docker containers to the cloud (Render, Fly.io, or VPS).
🧠 Core Concepts
💡 What is Docker?
Docker is a containerization tool that packages your application and its dependencies into a single unit called a container. Unlike traditional deployment methods, containers ensure your app runs the same across all environments—dev, test, or production.
🗃️ Key Docker Components:
-
Dockerfile – A script to build a Docker image.
-
Docker Image – A snapshot of your app and its environment.
-
Docker Container – A running instance of an image.
-
Docker Hub – A public/private registry to host and share images.
-
Docker Compose – A tool to manage multi-container apps using a YAML file.
🧪 Hands-On Walkthrough
🔧 Step 1: Install Docker
-
Download & install Docker Desktop from https://www.docker.com/products/docker-desktop/
-
Verify installation:
docker --version
📁 Step 2: Create Your Dockerfile
Example for a FastAPI backend:
# Use official Python base image
FROM python:3.11-slim
# Set working directory
WORKDIR /app
# Copy requirements and install
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Copy project files
COPY . .
# Run app
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
🧷 Step 3: Create Docker Compose File (Optional)
Useful for connecting backend + database or frontend:
version: "3.8"
services:
backend:
build: .
ports:
- "8000:8000"
env_file:
- .env
🔨 Step 4: Build & Run the Container Locally
# Build your image
docker build -t my-chatbot .
# Run it
docker run -p 8000:8000 my-chatbot
Or using Compose:
docker-compose up --build
Visit: http://localhost:8000
🛰️ Step 5: Push to Docker Hub
# Login to Docker Hub
docker login
# Tag your image
docker tag my-chatbot yourusername/my-chatbot:latest
# Push it
docker push yourusername/my-chatbot:latest
🌍 Step 6: Deploy to a Hosting Provider (Render or VPS)
Option A: Render (Docker Deploy)
-
Sign up at render.com.
-
Create a new Web Service.
-
Choose Docker as deployment method.
-
Connect GitHub repo or use Docker Hub image.
Option B: VPS (like DigitalOcean)
-
Spin up a server (Ubuntu).
-
Install Docker & Docker Compose:
sudo apt update sudo apt install docker.io docker-compose -
Clone your repo and run:
docker-compose up --build -d
🔐 Best Practices for Docker Hosting
-
Use
.envfiles for secrets and API keys. -
Keep your image size small by using
python:3.11-slimor Alpine images. -
Use multi-stage builds for better performance (for Node or React apps).
-
Expose only necessary ports (e.g., 8000).
-
Use
.dockerignoreto avoid copying unwanted files (like.gitor__pycache__).
Example .dockerignore:
__pycache__
*.pyc
.git
.env
📝 Assignments & Practice
-
✅ Dockerize your chatbot backend.
-
✅ Push the image to Docker Hub.
-
✅ Deploy it on Render using Docker.
📦 Downloads Included
-
Sample
Dockerfile(FastAPI) -
Sample
docker-compose.yml -
.dockerignoretemplate -
Step-by-step PDF deployment guide
📚 Further Learning
92
