Skip to content

Commit 3945c19

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

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed

Dockerfile

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,18 @@
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 . .
198
RUN pnpm build
209

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"]
10+
# 運行階段
11+
FROM nginx:alpine
12+
# 確保完整複製所有檔案
13+
COPY --from=build /app/build/client /usr/share/nginx/html/
14+
COPY nginx.conf /etc/nginx/conf.d/default.conf
15+
16+
# 顯示檔案結構(除錯用)
17+
RUN ls -la /usr/share/nginx/html
18+
RUN ls -la /usr/share/nginx/html/assets || true

nginx.conf

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
server {
2+
listen 80;
3+
include /etc/nginx/mime.types;
4+
5+
root /usr/share/nginx/html;
6+
index index.html;
7+
8+
# 處理帶有 base path 的請求
9+
location /react-main-2024-w2 {
10+
alias /usr/share/nginx/html;
11+
try_files $uri $uri/ /index.html;
12+
13+
# 處理 JavaScript 模組
14+
location ~ \.js$ {
15+
add_header Content-Type "application/javascript";
16+
}
17+
}
18+
19+
# 處理 assets 目錄
20+
location /react-main-2024-w2/assets/ {
21+
alias /usr/share/nginx/html/assets/;
22+
add_header Cache-Control "public, max-age=31536000, immutable";
23+
}
24+
25+
# 根路徑重定向到帶有 base path 的路徑
26+
location = / {
27+
return 301 /react-main-2024-w2/;
28+
}
29+
}

0 commit comments

Comments
 (0)