Skip to content

Commit 946e85d

Browse files
committed
build(docker): Update to refactor Dockerfile for multi-stage build and add nginx configuration
1 parent 88c9eca commit 946e85d

File tree

2 files changed

+79
-21
lines changed

2 files changed

+79
-21
lines changed

Dockerfile

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
1-
FROM node:20-alpine AS dependencies-env
1+
# 建置階段
2+
FROM node:20-alpine AS build
23
RUN npm i -g pnpm
3-
COPY . /app
4-
5-
FROM dependencies-env AS development-dependencies-env
6-
COPY ./package.json pnpm-lock.yaml /app/
74
WORKDIR /app
5+
COPY package.json pnpm-lock.yaml ./
86
RUN pnpm i --frozen-lockfile
9-
10-
FROM dependencies-env AS production-dependencies-env
11-
COPY ./package.json pnpm-lock.yaml /app/
12-
WORKDIR /app
13-
RUN pnpm i --prod --frozen-lockfile
14-
15-
FROM dependencies-env AS build-env
16-
COPY ./package.json pnpm-lock.yaml /app/
17-
COPY --from=development-dependencies-env /app/node_modules /app/node_modules
18-
WORKDIR /app
7+
COPY . .
8+
ARG VITE_APP_API_URL
9+
ENV VITE_APP_API_URL=${VITE_APP_API_URL}
1910
RUN pnpm build
2011

21-
FROM dependencies-env
22-
COPY ./package.json pnpm-lock.yaml /app/
23-
COPY --from=production-dependencies-env /app/node_modules /app/node_modules
24-
COPY --from=build-env /app/build /app/build
25-
WORKDIR /app
26-
CMD ["pnpm", "start"]
12+
# 運行階段
13+
FROM nginx:alpine
14+
# 複製檔案
15+
COPY --from=build /app/build/client /usr/share/nginx/html/
16+
# 複製 nginx 配置模板
17+
COPY nginx.conf /etc/nginx/templates/default.conf.template
18+
# 設定代理 URL
19+
ENV proxy_url=${VITE_APP_API_URL}
20+
# 建立日誌目錄
21+
RUN mkdir -p /var/log/nginx
22+
# 使用 envsubst 來替換環境變數
23+
CMD ["/bin/sh", "-c", "envsubst '${proxy_url}' < /etc/nginx/templates/default.conf.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"]

nginx.conf

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
server {
2+
listen 80;
3+
include /etc/nginx/mime.types;
4+
5+
root /usr/share/nginx/html;
6+
index index.html;
7+
8+
# 詳細的除錯日誌
9+
error_log /var/log/nginx/error.log debug;
10+
access_log /var/log/nginx/access.log;
11+
12+
# API Proxy 設定
13+
location /v2/ {
14+
resolver 8.8.8.8;
15+
proxy_pass $proxy_url;
16+
17+
# 基本代理設定
18+
proxy_http_version 1.1;
19+
proxy_set_header Host $proxy_host;
20+
proxy_set_header X-Real-IP $remote_addr;
21+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
22+
proxy_set_header X-Forwarded-Proto $scheme;
23+
24+
# 增加超時設定
25+
proxy_connect_timeout 60;
26+
proxy_send_timeout 60;
27+
proxy_read_timeout 60;
28+
29+
# 除錯用設定
30+
proxy_intercept_errors on;
31+
error_log /var/log/nginx/api_error.log debug;
32+
}
33+
34+
# 處理帶有 base path 的請求
35+
location /react-main-2024-w2 {
36+
alias /usr/share/nginx/html;
37+
try_files $uri $uri/ /index.html;
38+
39+
# 處理 JavaScript 檔案
40+
location ~ \.js$ {
41+
add_header Content-Type "application/javascript";
42+
}
43+
}
44+
45+
# 處理 assets 目錄
46+
location /react-main-2024-w2/assets/ {
47+
alias /usr/share/nginx/html/assets/;
48+
add_header Cache-Control "public, max-age=31536000, immutable";
49+
}
50+
51+
# 根路徑重定向到帶有 base path 的路徑
52+
location = / {
53+
return 301 /react-main-2024-w2/;
54+
}
55+
56+
# 錯誤頁面設定
57+
error_page 500 502 503 504 /50x.html;
58+
location = /50x.html {
59+
root /usr/share/nginx/html;
60+
}
61+
}

0 commit comments

Comments
 (0)