Quellcode durchsuchen

fix(docker): fix production API URL build arg and add .dockerignore to exclude local env

jerry vor 2 Monaten
Ursprung
Commit
b21b7ca117
5 geänderte Dateien mit 15 neuen und 6 gelöschten Zeilen
  1. 5 0
      .dockerignore
  2. 2 0
      .env.production
  3. 2 0
      Dockerfile
  4. 4 4
      docker-compose.yml
  5. 2 2
      src/lib/api.js

+ 5 - 0
.dockerignore

@@ -0,0 +1,5 @@
+node_modules
+.next
+.git
+.env*.local
+.env.development

+ 2 - 0
.env.production

@@ -0,0 +1,2 @@
+# .env.production
+NEXT_PUBLIC_API_URL=https://api.text.skin

+ 2 - 0
Dockerfile

@@ -8,6 +8,8 @@ FROM node:18-alpine AS builder
 WORKDIR /app
 COPY --from=deps /app/node_modules ./node_modules
 COPY . .
+ARG NEXT_PUBLIC_API_URL=https://api.text.skin
+ENV NEXT_PUBLIC_API_URL=$NEXT_PUBLIC_API_URL
 ENV NEXT_TELEMETRY_DISABLED 1
 RUN npm run build
 

+ 4 - 4
docker-compose.yml

@@ -5,13 +5,13 @@ services:
     container_name: next-web-ui
     build:
       context: .
-      dockerfile: Dockerfile    
+      dockerfile: Dockerfile
+      args:
+        - NEXT_PUBLIC_API_URL=${NEXT_PUBLIC_API_URL:-https://api.text.skin}
     restart: always
     ports:
-      # 格式: "宿主机IP:宿主机端口:容器端口"
       # 将容器的 3000 映射到宿主机的 3000,且仅限本机访问
       - "127.0.0.1:3000:3000"
     environment:
       - NEXT_DISABLE_ESLINT=1
-      - NODE_ENV=production
-      # 如果需要连接同在宿主机上的后端(非Docker),可以使用 host.docker.internal (需配置) 或直接填公网/局域网IP
+      - NODE_ENV=production

+ 2 - 2
src/lib/api.js

@@ -2,8 +2,8 @@ import axios from 'axios';
 
 // 1. 创建 axios 实例
 const api = axios.create({
-  // 根据你的环境变量或硬编码地址
-  baseURL: process.env.NEXT_PUBLIC_API_URL || 'http://45.137.220.138:8888',
+  // 根据环境变量或默认线上生产地址
+  baseURL: process.env.NEXT_PUBLIC_API_URL || 'https://api.text.skin',
   headers: {
     'Content-Type': 'application/json',
   },