diff --git a/Dockerfile.app b/Dockerfile.app index 524bbdc..71075a3 100644 --- a/Dockerfile.app +++ b/Dockerfile.app @@ -1,6 +1,6 @@ # 运行镜像:从 base 提取产物到精简运行时 # 构建命令: docker build -f Dockerfile.app -t aether-app:latest . -# 代码变更只需重建此镜像,无需重建 base +# 用于 GitHub Actions CI(官方源) FROM aether-base:latest AS builder WORKDIR /app diff --git a/Dockerfile.app.local b/Dockerfile.app.local new file mode 100644 index 0000000..0a0adee --- /dev/null +++ b/Dockerfile.app.local @@ -0,0 +1,135 @@ +# 运行镜像:从 base 提取产物到精简运行时(国内镜像源版本) +# 构建命令: docker build -f Dockerfile.app.local -t aether-app:latest . +# 用于本地/国内服务器部署 +FROM aether-base:latest AS builder + +WORKDIR /app + +# 复制前端源码并构建 +COPY frontend/ ./frontend/ +RUN cd frontend && npm run build + +# ==================== 运行时镜像 ==================== +FROM python:3.12-slim + +WORKDIR /app + +# 运行时依赖(使用清华镜像源) +RUN sed -i 's/deb.debian.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list.d/debian.sources && \ + apt-get update && apt-get install -y \ + nginx \ + supervisor \ + libpq5 \ + curl \ + && rm -rf /var/lib/apt/lists/* + +# 从 base 镜像复制 Python 包 +COPY --from=builder /usr/local/lib/python3.12/site-packages /usr/local/lib/python3.12/site-packages + +# 只复制需要的 Python 可执行文件 +COPY --from=builder /usr/local/bin/gunicorn /usr/local/bin/ +COPY --from=builder /usr/local/bin/uvicorn /usr/local/bin/ +COPY --from=builder /usr/local/bin/alembic /usr/local/bin/ + +# 从 builder 阶段复制前端构建产物 +COPY --from=builder /app/frontend/dist /usr/share/nginx/html + +# 复制后端代码 +COPY src/ ./src/ +COPY alembic.ini ./ +COPY alembic/ ./alembic/ + +# Nginx 配置模板 +RUN printf '%s\n' \ +'server {' \ +' listen 80;' \ +' server_name _;' \ +' root /usr/share/nginx/html;' \ +' index index.html;' \ +' client_max_body_size 100M;' \ +'' \ +' location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {' \ +' expires 1y;' \ +' add_header Cache-Control "public, no-transform";' \ +' try_files $uri =404;' \ +' }' \ +'' \ +' location ~ ^/(src|node_modules)/ {' \ +' deny all;' \ +' return 404;' \ +' }' \ +'' \ +' location ~ ^/(dashboard|admin|login)(/|$) {' \ +' try_files $uri $uri/ /index.html;' \ +' }' \ +'' \ +' location / {' \ +' try_files $uri $uri/ @backend;' \ +' }' \ +'' \ +' location @backend {' \ +' proxy_pass http://127.0.0.1:PORT_PLACEHOLDER;' \ +' proxy_http_version 1.1;' \ +' 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;' \ +' proxy_set_header Connection "";' \ +' proxy_set_header Accept $http_accept;' \ +' proxy_set_header Content-Type $content_type;' \ +' proxy_set_header Authorization $http_authorization;' \ +' proxy_set_header X-Api-Key $http_x_api_key;' \ +' proxy_buffering off;' \ +' proxy_cache off;' \ +' proxy_request_buffering off;' \ +' chunked_transfer_encoding on;' \ +' gzip off;' \ +' add_header X-Accel-Buffering no;' \ +' proxy_connect_timeout 600s;' \ +' proxy_send_timeout 600s;' \ +' proxy_read_timeout 600s;' \ +' }' \ +'}' > /etc/nginx/sites-available/default.template + +# Supervisor 配置 +RUN printf '%s\n' \ +'[supervisord]' \ +'nodaemon=true' \ +'logfile=/var/log/supervisor/supervisord.log' \ +'pidfile=/var/run/supervisord.pid' \ +'' \ +'[program:nginx]' \ +'command=/bin/bash -c "sed \"s/PORT_PLACEHOLDER/${PORT:-8084}/g\" /etc/nginx/sites-available/default.template > /etc/nginx/sites-available/default && /usr/sbin/nginx -g \"daemon off;\""' \ +'autostart=true' \ +'autorestart=true' \ +'stdout_logfile=/var/log/nginx/access.log' \ +'stderr_logfile=/var/log/nginx/error.log' \ +'' \ +'[program:app]' \ +'command=gunicorn src.main:app -w %(ENV_GUNICORN_WORKERS)s -k uvicorn.workers.UvicornWorker --bind 0.0.0.0:%(ENV_PORT)s --timeout 120 --access-logfile - --error-logfile - --log-level info' \ +'directory=/app' \ +'autostart=true' \ +'autorestart=true' \ +'stdout_logfile=/dev/stdout' \ +'stdout_logfile_maxbytes=0' \ +'stderr_logfile=/dev/stderr' \ +'stderr_logfile_maxbytes=0' \ +'environment=PYTHONUNBUFFERED=1,PYTHONIOENCODING=utf-8,LANG=C.UTF-8,LC_ALL=C.UTF-8,DOCKER_CONTAINER=true' > /etc/supervisor/conf.d/supervisord.conf + +# 创建目录 +RUN mkdir -p /var/log/supervisor /app/logs /app/data + +# 环境变量 +ENV PYTHONUNBUFFERED=1 \ + PYTHONDONTWRITEBYTECODE=1 \ + PYTHONIOENCODING=utf-8 \ + LANG=C.UTF-8 \ + LC_ALL=C.UTF-8 \ + PORT=8084 + +EXPOSE 80 + +HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \ + CMD curl -f http://localhost/health || exit 1 + +CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"] diff --git a/Dockerfile.base b/Dockerfile.base index 8f798ce..c7c74eb 100644 --- a/Dockerfile.base +++ b/Dockerfile.base @@ -23,8 +23,3 @@ RUN mkdir -p src && touch src/__init__.py && \ # 前端依赖(只安装,不构建) COPY frontend/package*.json ./frontend/ RUN cd frontend && npm ci - -# 产物位置: -# - Python 包: /usr/local/lib/python3.12/site-packages -# - Python 可执行文件: /usr/local/bin -# - 前端 node_modules: /app/frontend/node_modules diff --git a/Dockerfile.base.local b/Dockerfile.base.local index be126df..e244804 100644 --- a/Dockerfile.base.local +++ b/Dockerfile.base.local @@ -26,8 +26,3 @@ RUN mkdir -p src && touch src/__init__.py && \ # 前端依赖(只安装,不构建,使用淘宝镜像源) COPY frontend/package*.json ./frontend/ RUN cd frontend && npm config set registry https://registry.npmmirror.com && npm ci - -# 产物位置: -# - Python 包: /usr/local/lib/python3.12/site-packages -# - Python 可执行文件: /usr/local/bin -# - 前端 node_modules: /app/frontend/node_modules diff --git a/deploy.sh b/deploy.sh index f372c3f..b4d5e58 100755 --- a/deploy.sh +++ b/deploy.sh @@ -88,7 +88,7 @@ build_base() { # 构建应用镜像 build_app() { echo ">>> Building app image (code only)..." - docker build -f Dockerfile.app -t aether-app:latest . + docker build -f Dockerfile.app.local -t aether-app:latest . save_code_hash } diff --git a/docker-compose.build.yml b/docker-compose.build.yml index e89a878..90c7d32 100644 --- a/docker-compose.build.yml +++ b/docker-compose.build.yml @@ -41,7 +41,7 @@ services: app: build: context: . - dockerfile: Dockerfile.app + dockerfile: Dockerfile.app.local image: aether-app:latest container_name: aether-app environment: