refactor: split back into two containers (backend + frontend)
Single-container supervisord approach added unnecessary complexity. Two containers is simpler and more standard: - Dockerfile.backend: python:3.11-slim, uvicorn on port 8000 - Dockerfile.frontend: node build → nginx:alpine on port 80 - nginx.conf: proxy_pass restored to http://backend:8000 - docker-compose.yml: two services with depends_on - Removed combined Dockerfile and supervisord.conf Each container does one thing; logs are separate; either can be restarted independently. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,55 +0,0 @@
|
|||||||
# ── Stage 1: Build React frontend ─────────────────────────────────────────
|
|
||||||
FROM node:20-alpine AS frontend-builder
|
|
||||||
|
|
||||||
WORKDIR /app
|
|
||||||
COPY frontend/package*.json ./
|
|
||||||
RUN npm ci
|
|
||||||
COPY frontend/ .
|
|
||||||
RUN npm run build
|
|
||||||
|
|
||||||
# ── Stage 2: Single runtime image ─────────────────────────────────────────
|
|
||||||
FROM python:3.11-slim
|
|
||||||
|
|
||||||
ENV PYTHONUNBUFFERED=1 \
|
|
||||||
PYTHONDONTWRITEBYTECODE=1 \
|
|
||||||
PIP_NO_CACHE_DIR=1
|
|
||||||
|
|
||||||
# Install nginx, supervisor, and gcc (for some pip packages)
|
|
||||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
|
||||||
nginx \
|
|
||||||
supervisor \
|
|
||||||
gcc \
|
|
||||||
git \
|
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
|
||||||
|
|
||||||
WORKDIR /app
|
|
||||||
|
|
||||||
# Python dependencies
|
|
||||||
COPY requirements.txt .
|
|
||||||
RUN pip install --no-cache-dir -r requirements.txt
|
|
||||||
|
|
||||||
# Backend source
|
|
||||||
COPY api_server.py airports.py cache.py ./
|
|
||||||
COPY database/ ./database/
|
|
||||||
|
|
||||||
# Frontend build output
|
|
||||||
COPY --from=frontend-builder /app/dist /usr/share/nginx/html
|
|
||||||
|
|
||||||
# Config files
|
|
||||||
COPY nginx.conf /etc/nginx/conf.d/default.conf
|
|
||||||
COPY supervisord.conf /etc/supervisor/conf.d/app.conf
|
|
||||||
|
|
||||||
# Remove the default nginx site
|
|
||||||
RUN rm -f /etc/nginx/sites-enabled/default
|
|
||||||
|
|
||||||
# Pre-fetch airport data and initialise the database at build time
|
|
||||||
RUN mkdir -p data && \
|
|
||||||
python -c "from airports import download_and_build_airport_data; download_and_build_airport_data()" && \
|
|
||||||
python database/init_db.py
|
|
||||||
|
|
||||||
EXPOSE 80
|
|
||||||
|
|
||||||
HEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3 \
|
|
||||||
CMD wget -q --spider http://localhost/ || exit 1
|
|
||||||
|
|
||||||
CMD ["/usr/bin/supervisord", "-n", "-c", "/etc/supervisor/conf.d/app.conf"]
|
|
||||||
30
flight-comparator/Dockerfile.backend
Normal file
30
flight-comparator/Dockerfile.backend
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
FROM python:3.11-slim
|
||||||
|
|
||||||
|
ENV PYTHONUNBUFFERED=1 \
|
||||||
|
PYTHONDONTWRITEBYTECODE=1 \
|
||||||
|
PIP_NO_CACHE_DIR=1
|
||||||
|
|
||||||
|
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||||
|
gcc \
|
||||||
|
git \
|
||||||
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
COPY requirements.txt .
|
||||||
|
RUN pip install --no-cache-dir -r requirements.txt
|
||||||
|
|
||||||
|
COPY api_server.py airports.py cache.py ./
|
||||||
|
COPY database/ ./database/
|
||||||
|
|
||||||
|
# Pre-fetch airport data and initialise the database at build time
|
||||||
|
RUN mkdir -p data && \
|
||||||
|
python -c "from airports import download_and_build_airport_data; download_and_build_airport_data()" && \
|
||||||
|
python database/init_db.py
|
||||||
|
|
||||||
|
EXPOSE 8000
|
||||||
|
|
||||||
|
HEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3 \
|
||||||
|
CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8000/health')" || exit 1
|
||||||
|
|
||||||
|
CMD ["python", "api_server.py"]
|
||||||
19
flight-comparator/Dockerfile.frontend
Normal file
19
flight-comparator/Dockerfile.frontend
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
# ── Stage 1: Build React frontend ─────────────────────────────────────────
|
||||||
|
FROM node:20-alpine AS builder
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
COPY frontend/package*.json ./
|
||||||
|
RUN npm ci
|
||||||
|
COPY frontend/ .
|
||||||
|
RUN npm run build
|
||||||
|
|
||||||
|
# ── Stage 2: Serve with nginx ──────────────────────────────────────────────
|
||||||
|
FROM nginx:alpine
|
||||||
|
|
||||||
|
COPY --from=builder /app/dist /usr/share/nginx/html
|
||||||
|
COPY nginx.conf /etc/nginx/conf.d/default.conf
|
||||||
|
|
||||||
|
EXPOSE 80
|
||||||
|
|
||||||
|
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
|
||||||
|
CMD wget -q --spider http://localhost/ || exit 1
|
||||||
@@ -1,15 +1,28 @@
|
|||||||
services:
|
services:
|
||||||
app:
|
backend:
|
||||||
build: .
|
build:
|
||||||
container_name: flight-radar
|
context: .
|
||||||
|
dockerfile: Dockerfile.backend
|
||||||
|
container_name: flight-radar-backend
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
ports:
|
ports:
|
||||||
- "80:80"
|
- "8000:8000"
|
||||||
environment:
|
environment:
|
||||||
- DATABASE_PATH=/app/data/cache.db
|
- DATABASE_PATH=/app/data/cache.db
|
||||||
volumes:
|
volumes:
|
||||||
- flight-radar-data:/app/data
|
- flight-radar-data:/app/data
|
||||||
|
|
||||||
|
frontend:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: Dockerfile.frontend
|
||||||
|
container_name: flight-radar-frontend
|
||||||
|
restart: unless-stopped
|
||||||
|
ports:
|
||||||
|
- "80:80"
|
||||||
|
depends_on:
|
||||||
|
- backend
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
flight-radar-data:
|
flight-radar-data:
|
||||||
driver: local
|
driver: local
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ server {
|
|||||||
|
|
||||||
# API proxy
|
# API proxy
|
||||||
location /api/ {
|
location /api/ {
|
||||||
proxy_pass http://localhost:8000;
|
proxy_pass http://backend:8000;
|
||||||
proxy_http_version 1.1;
|
proxy_http_version 1.1;
|
||||||
proxy_set_header Upgrade $http_upgrade;
|
proxy_set_header Upgrade $http_upgrade;
|
||||||
proxy_set_header Connection 'upgrade';
|
proxy_set_header Connection 'upgrade';
|
||||||
@@ -30,7 +30,7 @@ server {
|
|||||||
|
|
||||||
# Health check endpoint proxy
|
# Health check endpoint proxy
|
||||||
location /health {
|
location /health {
|
||||||
proxy_pass http://localhost:8000;
|
proxy_pass http://backend:8000;
|
||||||
proxy_http_version 1.1;
|
proxy_http_version 1.1;
|
||||||
proxy_set_header Host $host;
|
proxy_set_header Host $host;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,24 +0,0 @@
|
|||||||
[supervisord]
|
|
||||||
nodaemon=true
|
|
||||||
logfile=/dev/null
|
|
||||||
logfile_maxbytes=0
|
|
||||||
pidfile=/tmp/supervisord.pid
|
|
||||||
|
|
||||||
[program:api]
|
|
||||||
command=python /app/api_server.py
|
|
||||||
directory=/app
|
|
||||||
autostart=true
|
|
||||||
autorestart=true
|
|
||||||
stdout_logfile=/dev/stdout
|
|
||||||
stdout_logfile_maxbytes=0
|
|
||||||
stderr_logfile=/dev/stderr
|
|
||||||
stderr_logfile_maxbytes=0
|
|
||||||
|
|
||||||
[program:nginx]
|
|
||||||
command=nginx -g "daemon off;"
|
|
||||||
autostart=true
|
|
||||||
autorestart=true
|
|
||||||
stdout_logfile=/dev/stdout
|
|
||||||
stdout_logfile_maxbytes=0
|
|
||||||
stderr_logfile=/dev/stderr
|
|
||||||
stderr_logfile_maxbytes=0
|
|
||||||
Reference in New Issue
Block a user