From 15e71a92311046a36e1077b2d4acab4544e86820 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 12 Apr 2026 20:18:09 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=89=8D=E7=AB=AF?= =?UTF-8?q?=E8=AE=BF=E9=97=AE=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docker-compose.yml | 15 ++++++++++++++- frontend/.dockerignore | 7 +++++++ frontend/Dockerfile | 4 +++- start.sh | 22 +++++++++++++++++++--- 4 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 frontend/.dockerignore diff --git a/docker-compose.yml b/docker-compose.yml index 7f8833e..a19c260 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -22,6 +22,18 @@ services: context: ./backend container_name: exam-helper-backend restart: unless-stopped + healthcheck: + test: + [ + "CMD", + "python", + "-c", + "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8000/health', timeout=3).read()", + ] + interval: 5s + timeout: 5s + retries: 12 + start_period: 25s # AI 相关变量优先从 .env.example 注入;若存在 .env 则覆盖同名项(见 Compose env_file 顺序) env_file: - .env.example @@ -46,7 +58,8 @@ services: container_name: exam-helper-frontend restart: unless-stopped depends_on: - - backend + backend: + condition: service_healthy ports: - "127.0.0.1:8173:80" diff --git a/frontend/.dockerignore b/frontend/.dockerignore new file mode 100644 index 0000000..79a9360 --- /dev/null +++ b/frontend/.dockerignore @@ -0,0 +1,7 @@ +node_modules +dist +.git +.gitignore +*.md +.DS_Store +npm-debug.log* diff --git a/frontend/Dockerfile b/frontend/Dockerfile index 9aa3d14..f9f7458 100644 --- a/frontend/Dockerfile +++ b/frontend/Dockerfile @@ -9,7 +9,9 @@ COPY package.json /app/package.json RUN npm config set registry https://registry.npmmirror.com && npm install COPY . /app -RUN npm run build +RUN npm run build \ + && test -f /app/dist/index.html \ + && ls -la /app/dist FROM nginx:1.27-alpine RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories diff --git a/start.sh b/start.sh index fd23bd8..76e810a 100755 --- a/start.sh +++ b/start.sh @@ -18,8 +18,24 @@ else echo "==> AI 配置来自 .env.example(请填写 QWEN_API_KEY;亦可复制为 .env 再填写)" fi -echo "==> 健康检查(端口与 docker-compose.yml 一致:后端 8866,前端 Nginx 8173)" -echo "backend: $(curl -sf http://localhost:8866/health || echo 'unreachable')" -echo "frontend (nginx): $(curl -sf http://localhost:8173/health || echo 'unreachable')" +wait_http() { + local url=$1 + local label=$2 + local i=0 + while [[ $i -lt 45 ]]; do + if curl -sf "$url" >/dev/null 2>&1; then + echo "${label}: ok" + return 0 + fi + sleep 1 + i=$((i + 1)) + done + echo "${label}: unreachable(请确认容器已就绪,或执行: curl -v ${url})" + return 1 +} + +echo "==> 健康检查(后端 8866,前端 Nginx 8173;最多等待约 45s)" +wait_http "http://localhost:8866/health" "backend" || true +wait_http "http://localhost:8173/health" "frontend (nginx)" || true echo "==> 启动完成"