From 49114c30a6a9dd98c64ce9269794e20d9b52e54f Mon Sep 17 00:00:00 2001 From: Amirhossein Khalili Date: Thu, 18 Jun 2026 20:58:28 +0330 Subject: [PATCH] feat: add dockerized demo deployment --- .dockerignore | 13 +++++++ DEPLOYMENT.md | 85 ++++++++++++++++++++++++++++++++++++++++++++++ Dockerfile | 15 ++++++++ docker-compose.yml | 30 ++++++++++++++++ nginx/default.conf | 35 +++++++++++++++++++ 5 files changed, 178 insertions(+) create mode 100644 .dockerignore create mode 100644 DEPLOYMENT.md create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100644 nginx/default.conf diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..31dc87d --- /dev/null +++ b/.dockerignore @@ -0,0 +1,13 @@ +.git/ +.venv/ +__pycache__/ +*.pyc +*.pyo +*.pyd +.pytest_cache/ +.mypy_cache/ +.ruff_cache/ +.uvicorn.pid + +docs/ +README.md diff --git a/DEPLOYMENT.md b/DEPLOYMENT.md new file mode 100644 index 0000000..d6e92a6 --- /dev/null +++ b/DEPLOYMENT.md @@ -0,0 +1,85 @@ +# Demo Deployment + +This project can run behind the existing Caddy edge service with this temporary route: + +```text +Caddy -> chatroom-nginx -> chatroom-app +``` + +The chatroom compose stack joins the existing external Docker network named `caddy_proxy`. No app ports are published to the host. + +## Start the Chatroom Stack + +From this project directory: + +```powershell +docker compose up -d --build +docker compose ps +``` + +Test the FastAPI health endpoint through the Nginx container: + +```powershell +docker compose exec chatroom-nginx wget -qO- http://chatroom-app:8000/health +``` + +## Temporary Caddy Change + +Do this manually in: + +```text +D:\Programing\Scripts\Home Lab\Interanet\caddy-deployment\config\caddy\Caddyfile +``` + +Add this temporary block: + +```caddy +chat.amiirkhl.ir { + import {$CADDY_TLS_MODE:custom_tls} + reverse_proxy chatroom-nginx:80 +} +``` + +If the current custom certificate does not include `chat.amiirkhl.ir`, use this instead: + +```caddy +chat.amiirkhl.ir { + import letsencrypt_tls + reverse_proxy chatroom-nginx:80 +} +``` + +Reload Caddy from the Caddy deployment directory: + +```powershell +docker compose exec caddy caddy reload --config /etc/caddy/Caddyfile +``` + +## Browser Test + +Open: + +```text +https://chat.amiirkhl.ir +``` + +Use two browser tabs, join the same room, and send messages both ways. The frontend builds its WebSocket URL from the current page, so HTTPS should connect with: + +```text +wss://chat.amiirkhl.ir/ws/{room}/{user} +``` + +## Undo After the Demo + +1. Remove the `chat.amiirkhl.ir` block from the Caddyfile. +2. Reload Caddy: + +```powershell +docker compose exec caddy caddy reload --config /etc/caddy/Caddyfile +``` + +3. Stop and remove the chatroom stack from this project directory: + +```powershell +docker compose down +``` diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..35e3db4 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,15 @@ +FROM python:3.12-slim + +ENV PYTHONDONTWRITEBYTECODE=1 \ + PYTHONUNBUFFERED=1 + +WORKDIR /app + +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt + +COPY app ./app + +EXPOSE 8000 + +CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--proxy-headers", "--forwarded-allow-ips", "*"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..67a6a68 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,30 @@ +services: + chatroom-app: + build: + context: . + image: chatroom-app:demo + restart: unless-stopped + expose: + - "8000" + networks: + - chatroom_internal + + chatroom-nginx: + image: nginx:alpine + restart: unless-stopped + depends_on: + - chatroom-app + expose: + - "80" + volumes: + - ./nginx/default.conf:/etc/nginx/conf.d/default.conf:ro + networks: + - chatroom_internal + - caddy_proxy + +networks: + chatroom_internal: + driver: bridge + caddy_proxy: + external: true + name: caddy_proxy diff --git a/nginx/default.conf b/nginx/default.conf new file mode 100644 index 0000000..aeb35b2 --- /dev/null +++ b/nginx/default.conf @@ -0,0 +1,35 @@ +map $http_upgrade $connection_upgrade { + default upgrade; + '' close; +} + +map $http_x_forwarded_proto $forwarded_proto { + default $http_x_forwarded_proto; + '' $scheme; +} + +upstream chatroom_app { + server chatroom-app:8000; +} + +server { + listen 80; + server_name _; + + location / { + proxy_pass http://chatroom_app; + proxy_http_version 1.1; + + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection $connection_upgrade; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $forwarded_proto; + proxy_set_header X-Forwarded-Host $host; + proxy_set_header X-Forwarded-Port $server_port; + + proxy_read_timeout 3600s; + proxy_send_timeout 3600s; + } +}