Просмотр исходного кода

Separate frontend and backend into two containers

- Frontend: nginx:alpine serving static files, proxying API to backend
- Backend: Python/uvicorn running FastAPI
- Remove start.sh (no longer needed)
- Update nginx.conf to proxy to 'backend' service
- Separate logs for easier debugging

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
tuanchris 3 недель назад
Родитель
Сommit
a3a4332dac
3 измененных файлов с 66 добавлено и 78 удалено
  1. 25 14
      docker-compose.yml
  2. 41 48
      nginx.conf
  3. 0 16
      start.sh

+ 25 - 14
docker-compose.yml

@@ -1,14 +1,25 @@
 services:
-  dune-weaver:
+  frontend:
+    image: nginx:alpine
+    restart: always
+    ports:
+      - "80:80"
+    volumes:
+      - ./static/dist:/usr/share/nginx/html:ro
+      - ./nginx.conf:/etc/nginx/conf.d/default.conf:ro
+    depends_on:
+      - backend
+    container_name: dune-weaver-frontend
+
+  backend:
     build: .
     image: ghcr.io/tuanchris/dune-weaver:main
     restart: always
-    command: /app/start.sh
     ports:
-      - "80:80"     # Frontend (nginx)
-      - "8080:8080" # Backend API
+      - "8080:8080"
+    command: uvicorn main:app --host 0.0.0.0 --port 8080
     volumes:
-      # Mount entire app directory for development
+      # Mount entire app directory
       - .:/app
       # Mount Docker socket for container self-restart/update
       - /var/run/docker.sock:/var/run/docker.sock
@@ -16,16 +27,16 @@ services:
       - /etc/timezone:/etc/host-timezone:ro
       # Mount systemd for host shutdown capability
       - /run/systemd/system:/run/systemd/system:ro
-      - /var/run/dbus/system_bus_socket:/var/run/dbus/system_bus_socket:ro
+      - /var/run/dbus/system_bus_socket:/var/run/dbus/system_socket:ro
       - /sys/fs/cgroup:/sys/fs/cgroup:ro
       # Mount GPIO for DW LEDs and Desert Compass (reed switch)
       - /sys:/sys
     devices:
-      - "/dev/ttyACM0:/dev/ttyACM0"  # Serial device for stepper motors
-      - "/dev/ttyUSB0:/dev/ttyUSB0"  # Alternative serial device
-      - "/dev/ttyAMA0:/dev/ttyAMA0"  # Alternative serial device
-      - "/dev/gpiomem:/dev/gpiomem"  # GPIO memory access for DW LEDs
-      - "/dev/mem:/dev/mem"          # Direct memory access for PWM
-      - "/dev/pio0:/dev/pio0"        # PIO device for Pi 5 NeoPixel support
-    privileged: true  # Required for GPIO/PWM access
-    container_name: dune-weaver
+      - "/dev/ttyACM0:/dev/ttyACM0"
+      - "/dev/ttyUSB0:/dev/ttyUSB0"
+      - "/dev/ttyAMA0:/dev/ttyAMA0"
+      - "/dev/gpiomem:/dev/gpiomem"
+      - "/dev/mem:/dev/mem"
+      - "/dev/pio0:/dev/pio0"
+    privileged: true
+    container_name: dune-weaver-backend

+ 41 - 48
nginx.conf

@@ -1,48 +1,41 @@
-server {
-    listen 80;
-    server_name _;
-
-    # Frontend - serve static files
-    location / {
-        root /app/static/dist;
-        index index.html;
-        try_files $uri $uri/ /index.html;
-    }
-
-    # Serve static assets (legacy CSS, images, etc.)
-    location /static/ {
-        alias /app/static/;
-        expires 1d;
-        add_header Cache-Control "public, immutable";
-    }
-
-    # API proxy to backend
-    location /api/ {
-        proxy_pass http://127.0.0.1:8080;
-        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 $scheme;
-    }
-
-    # WebSocket proxy
-    location /ws/ {
-        proxy_pass http://127.0.0.1:8080;
-        proxy_http_version 1.1;
-        proxy_set_header Upgrade $http_upgrade;
-        proxy_set_header 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_read_timeout 86400;
-    }
-
-    # Legacy endpoints (non-/api/ routes that go to backend)
-    location ~ ^/(preview_thr_batch|get_theta_rho_coordinates|pause_execution|resume_execution|stop_execution|skip_pattern|set_speed|restart|shutdown|run_pattern|run_playlist|connect_device|disconnect_device|home_device|clear_sand) {
-        proxy_pass http://127.0.0.1:8080;
-        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 $scheme;
-    }
-}
+server {
+    listen 80;
+    server_name _;
+
+    # Frontend - serve static files
+    location / {
+        root /usr/share/nginx/html;
+        index index.html;
+        try_files $uri $uri/ /index.html;
+    }
+
+    # API proxy to backend container
+    location /api/ {
+        proxy_pass http://backend:8080;
+        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 $scheme;
+    }
+
+    # WebSocket proxy
+    location /ws/ {
+        proxy_pass http://backend:8080;
+        proxy_http_version 1.1;
+        proxy_set_header Upgrade $http_upgrade;
+        proxy_set_header 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_read_timeout 86400;
+    }
+
+    # Legacy endpoints (non-/api/ routes that go to backend)
+    location ~ ^/(preview_thr_batch|get_theta_rho_coordinates|pause_execution|resume_execution|stop_execution|skip_pattern|set_speed|restart|shutdown|run_pattern|run_playlist|connect_device|disconnect_device|home_device|clear_sand) {
+        proxy_pass http://backend:8080;
+        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 $scheme;
+    }
+}

+ 0 - 16
start.sh

@@ -1,16 +0,0 @@
-#!/bin/sh
-set -e
-
-# Start nginx in background
-nginx &
-NGINX_PID=$!
-
-# Start backend
-uvicorn main:app --host 0.0.0.0 --port 8080 &
-UVICORN_PID=$!
-
-# Wait for either process to exit
-wait $NGINX_PID $UVICORN_PID
-
-# If one exits, kill the other
-kill $NGINX_PID $UVICORN_PID 2>/dev/null || true