diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..6054892 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +node_modules +frontend/node_modules +frontend/dist +backend/bin +.git diff --git a/backend/Dockerfile b/backend/Dockerfile new file mode 100644 index 0000000..63a0ad5 --- /dev/null +++ b/backend/Dockerfile @@ -0,0 +1,18 @@ +# ---- Build Stage ---- +FROM golang:1.26-alpine AS builder + +WORKDIR /src +COPY go.mod go.sum ./ +RUN go mod download + +COPY . . +RUN CGO_ENABLED=0 GOOS=linux go build -o /bin/gen2d ./cmd/main.go + +# ---- Runtime Stage ---- +FROM alpine:3.20 + +RUN apk add --no-cache ca-certificates +COPY --from=builder /bin/gen2d /usr/local/bin/gen2d + +EXPOSE 8080 +CMD ["gen2d"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..2e14c32 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,21 @@ +services: + backend: + build: + context: ./backend + dockerfile: Dockerfile + environment: + - GEN2D_MODE=release + - GEN2D_PORT=8080 + expose: + - "8080" + restart: unless-stopped + + frontend: + build: + context: ./frontend + dockerfile: Dockerfile + ports: + - "10000:80" + depends_on: + - backend + restart: unless-stopped diff --git a/frontend/Dockerfile b/frontend/Dockerfile new file mode 100644 index 0000000..405dfe0 --- /dev/null +++ b/frontend/Dockerfile @@ -0,0 +1,17 @@ +# ---- Build Stage ---- +FROM node:20-alpine AS builder + +WORKDIR /app +COPY package.json package-lock.json* ./ +RUN npm ci + +COPY . . +RUN npm run build + +# ---- Runtime Stage ---- +FROM nginx:alpine + +COPY --from=builder /app/dist /usr/share/nginx/html +COPY nginx.conf /etc/nginx/conf.d/default.conf + +EXPOSE 80 diff --git a/frontend/nginx.conf b/frontend/nginx.conf new file mode 100644 index 0000000..6eb4544 --- /dev/null +++ b/frontend/nginx.conf @@ -0,0 +1,21 @@ +server { + listen 80; + server_name _; + + root /usr/share/nginx/html; + index index.html; + + # SPA fallback + location / { + try_files $uri $uri/ /index.html; + } + + # 反向代理 /api 到后端服务 + location /api/ { + proxy_pass http://backend:8080; + 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; + } +}