Building a Chatbot from Scratch: Part 6 - Deployment
The final chapter: deploying our chatbot to the world with Docker and a VPS.
Series Complete!
- [Part 1](/blog/building-chatbot-from-scratch-part-1-setup): Project setup
- [Part 2](/blog/building-chatbot-from-scratch-part-2-ui): Chat UI
- [Part 3](/blog/building-chatbot-part-3-websockets): WebSockets
- [Part 4](/blog/building-chatbot-part-4-adding-intelligence): Response logic
- [Part 5](/blog/building-chatbot-part-5-styling): Styling
Now let's deploy it.
Dockerizing
Create `Dockerfile`:
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
EXPOSE 3000
CMD ["node", "server/index.js"]
Create `docker-compose.yml`:
version: "3.8"
services:
chatbot:
build: .
ports:
- "3000:3000"
environment:
- NODE_ENV=production
restart: unless-stopped
Build and test locally:
docker compose up --build
Setting Up the VPS
I use DigitalOcean. A $6/month droplet works fine.
SSH in and install Docker:
# Update system
apt update && apt upgrade -y
# Install Docker
curl -fsSL https://get.docker.com | sh
# Install Docker Compose
apt install docker-compose-plugin
Deploying
Option 1: Git Clone
git clone https://github.com/yourusername/chatbot.git
cd chatbot
docker compose up -d --build
Option 2: Docker Registry
Push to Docker Hub or GitHub Container Registry:
docker build -t yourusername/chatbot .
docker push yourusername/chatbot
Then on server:
docker pull yourusername/chatbot
docker run -d -p 3000:3000 yourusername/chatbot
Nginx Reverse Proxy
For a proper domain with SSL:
server {
listen 80;
server_name chatbot.yourdomain.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name chatbot.yourdomain.com;
ssl_certificate /etc/letsencrypt/live/chatbot.yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/chatbot.yourdomain.com/privkey.pem;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
}
The WebSocket headers (`Upgrade`, `Connection`) are essential for Socket.io.
SSL with Certbot
apt install certbot python3-certbot-nginx
certbot --nginx -d chatbot.yourdomain.com
Free SSL, auto-renewal.
It's Live!
Visit your domain and chat with your bot!
What We Built
From scratch:
- Real-time chat with WebSockets
- Pattern-based responses
- Clean UI with animations
- Production deployment
Full code: github.com/chrishacia/simple-chatbot
Thanks for following along! Now go build something cool.