✅ Dockerfile + Deployment Guide for Your AI Chatbot API (DeepSeek Edition)

ic_date 2024-12-26
blogs

🔍 Introduction

With the rise of powerful local large language models (LLMs) like DeepSeek, developers are increasingly deploying their own chatbot APIs for privacy, control, and cost-efficiency. But to ensure scalability, portability, and easy updates, Docker is the go-to tool for containerizing and deploying such applications.

This guide provides a step-by-step walkthrough for containerizing your Python-based chatbot API (e.g., chatbot_api.py) and deploying it via Docker, Docker Compose, and optionally to cloud environments like AWS EC2, DigitalOcean, or VPS.

You’ll also learn how to:

  • Write a production-ready Dockerfile

  • Connect your Docker container to a local DeepSeek model via Ollama

  • Set environment variables

  • Configure HTTPS and reverse proxy via NGINX

  • Deploy to cloud with persistence and logs

  • Auto-restart on crash or reboot

✅ Table of Contents

  1. Why Use Docker for Chatbot Deployment?

  2. Project Prerequisites and Tools

  3. Directory Layout Overview

  4. Creating a Production-Ready Dockerfile

  5. Creating a docker-compose.yml

  6. Environment Variables via .env

  7. Running Locally with Ollama (DeepSeek)

  8. Setting Up NGINX Reverse Proxy (Optional)

  9. Hosting on Cloud/VPS

  10. Debugging and Logs

  11. Auto-Startup with Systemd or Watchtower

  12. Security Best Practices

  13. Conclusion + Full Download Pack

1. 🧱 Why Use Docker for Deployment?

BenefitsExplanation
PortabilityRun on any OS with Docker (Linux, Windows, macOS)
ReproducibilitySame environment everywhere—no “works on my machine” issues
Easy updatesJust rebuild and redeploy in seconds
IsolationKeeps dependencies separate
Infrastructure-readyEasily works with CI/CD, cloud, Kubernetes

2. ⚙️ Prerequisites and Tools

You’ll need:

  • A backend like chatbot_api.py built with FastAPI or Flask

  • Docker installed: Install Docker

  • (Optional) Docker Compose

  • Ollama with DeepSeek running locally or network-accessible

  • A Linux VPS (Ubuntu preferred) if deploying remotely

3. 📁 Project Directory Structure

arduino
chatbot-docker/
├── app/
│   ├── chatbot_api.py
│   ├── requirements.txt
├── Dockerfile
├── docker-compose.yml
├── .env
└── nginx/
    └── default.conf

4. 🐳 Creating a Dockerfile

Here’s a production-grade Dockerfile:

dockerfile
# Use official lightweight Python base image
FROM python:3.10-slim

# Set working directory
WORKDIR /app

# Copy app files
COPY app/ /app/

# Install dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Expose API port
EXPOSE 8000

# Set environment variables (can also use .env)
ENV HOST=0.0.0.0
ENV PORT=8000

# Run FastAPI app
CMD ["uvicorn", "chatbot_api:app", "--host", "0.0.0.0", "--port", "8000"]

5. 🧩 Creating docker-compose.yml

This helps manage multiple services (e.g., chatbot + Ollama + NGINX).

yaml
version: '3.9'services:
  chatbot:
    build: .
    ports:
      - "8000:8000"
    environment:
      - MODEL_NAME=deepseek-chat
      - API_KEY=secure123
    depends_on:
      - ollama
    restart: always

  ollama:
    image: ollama/ollama
    volumes:
      - ollama_data:/root/.ollama
    ports:
      - "11434:11434"volumes:
  ollama_data:

You must run ollama pull deepseek-chat on the host at least once before starting the containerized version.

6. 🧾 .env File (Optional)

Use this to store secrets or config cleanly:

ini
MODEL_NAME=deepseek-chatAPI_KEY=secure123

In your docker-compose.yml:

yaml
env_file:
  - .env

In Python (chatbot_api.py):

python
import os
model = os.getenv("MODEL_NAME")

7. 🚀 Running the App Locally

Run everything with one command:

bash
docker compose up --build -d

Check logs:

bash
docker compose logs -f chatbot

Test it:

bash
curl http://localhost:8000/chat -X POST \
  -H "Content-Type: application/json" \
  -d '{"user_id":"dev","message":"Hello"}'

You should get a JSON reply from DeepSeek.

8. 🌐 Setting Up NGINX Reverse Proxy (Optional)

nginx/default.conf

nginx
server {  listen 80;  server_name yourdomain.com;  location / {    proxy_pass http://chatbot:8000;    proxy_set_header Host $host;    proxy_set_header X-Real-IP $remote_addr;
  }
}

Add to Compose

yaml
  nginx:
    image: nginx
    volumes:
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    ports:
      - "80:80"
    depends_on:
      - chatbot

You now have http://yourdomain.com serving your API!

9. 🖥️ Hosting on Cloud/VPS

Steps:

  1. Spin up a server (Ubuntu 22.04 recommended)

  2. Install Docker and Docker Compose

  3. Upload your project via SCP or Git

  4. Run docker compose up -d

  5. (Optional) Set up domain and HTTPS with Caddy or Certbot

10. 🪵 Debugging and Logs

bash
docker logs chatbot
docker exec -it chatbot bash
docker top chatbot

Add log files:

yaml
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

11. 🔁 Auto-Restart and Systemd

Already handled in docker-compose.yml with:

yaml
restart: always

For system-level resilience:

bash
sudo systemctl enable docker

Or use Watchtower to auto-update containers:

yaml
  watchtower:
    image: containrrr/watchtower
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock

12. 🔐 Security Best Practices

ConcernRecommendation
Public API abuseAdd token or API key headers
DoS preventionRate limit via NGINX or API gateway
HTTPSUse Caddy or Certbot with NGINX
IsolationRun Docker with non-root user (optional)
LoggingSanitize logs and rotate them

13. ✅ Conclusion + Download Pack

By containerizing your chatbot API with Docker, you've unlocked a future-proof way to deploy and maintain your AI system:

  • Easy to scale

  • Runs locally or in cloud

  • Secure, customizable

  • Pairs perfectly with open-source LLMs like DeepSeek

📦 Included in the Download:

  • Dockerfile

  • docker-compose.yml

  • .env template

  • NGINX reverse proxy config

  • Bash deployment scripts

  • Postman collection

  • Sample Python + FastAPI chatbot API

Let me know if you'd like this as a GitHub repo, ZIP archive, or a DigitalOcean 1-click app image.

Would you like a follow-up guide on adding GPU support, LangChain integration, or deploying to Kubernetes?