From 8b4e0ffb09ff5102ef74d8db1aa2478f57065df4 Mon Sep 17 00:00:00 2001 From: mlogclub Date: Mon, 16 Sep 2024 02:04:53 +0800 Subject: [PATCH] docker --- .docker-compose/nginx.conf | 28 ++++++++++++++++++++++++++++ .dockerignore | 1 - Dockerfile | 26 ++++++++++++++++++++++---- admin/.dockerignore | 5 ----- admin/.env.docker | 2 ++ admin/Dockerfile | 19 ------------------- admin/config/vite.config.docker.ts | 1 + docker-compose.yml | 12 ++++++++++++ server/internal/server/server.go | 2 -- 9 files changed, 65 insertions(+), 31 deletions(-) create mode 100644 .docker-compose/nginx.conf delete mode 100644 admin/.dockerignore create mode 100644 admin/.env.docker delete mode 100644 admin/Dockerfile diff --git a/.docker-compose/nginx.conf b/.docker-compose/nginx.conf new file mode 100644 index 000000000..f465ccaec --- /dev/null +++ b/.docker-compose/nginx.conf @@ -0,0 +1,28 @@ +# nginx.conf +server { + listen 80; + + # 代理到 Node.js 服务 + location / { + proxy_pass http://bbsgo:3000; + 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; + } + + # 代理到 Go 服务 + location /api/ { + proxy_pass http://bbsgo:8082; + 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; + } + + # 代理到 Admin 静态资源 + location /admin/ { + alias /app/bbs-go/admin/; + try_files $uri $uri/ /index.html; + } +} diff --git a/.dockerignore b/.dockerignore index a831a2494..bc46c24eb 100644 --- a/.dockerignore +++ b/.dockerignore @@ -19,4 +19,3 @@ admin/node_modules admin/dist admin/dist-ssr admin/*.local - diff --git a/Dockerfile b/Dockerfile index ddb6584bc..10fed9bb0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,7 +14,7 @@ RUN CGO_ENABLED=0 go build -v -o bbs-go main.go && chmod +x bbs-go # site builder -FROM node:20-alpine AS site_builder +FROM node:20 AS site_builder ENV APP_HOME=/code/bbs-go/site WORKDIR "$APP_HOME" @@ -26,19 +26,37 @@ RUN npm install -g pnpm RUN pnpm install RUN pnpm build:docker +# admin builder +FROM node:20-alpine AS admin_builder + +ENV APP_HOME=/code/bbs-go/admin +WORKDIR "$APP_HOME" + +# 安装 jpegtran 所需库 +RUN apk add --no-cache libjpeg-turbo-utils + +COPY ./admin ./ +# RUN npm install -g pnpm --registry=https://registry.npmmirror.com +# RUN pnpm install --registry=https://registry.npmmirror.com +RUN npm install -g pnpm +RUN pnpm install +RUN pnpm build:docker + # run FROM node:20-alpine ENV APP_HOME=/app/bbs-go WORKDIR "$APP_HOME" -COPY --from=site_builder /code/bbs-go/site/.output ./site/.output -COPY --from=site_builder /code/bbs-go/site/node_modules ./site/node_modules + COPY --from=server_builder /code/bbs-go/server/bbs-go ./server/bbs-go COPY --from=server_builder /code/bbs-go/server/*.yaml ./server/ COPY --from=server_builder /code/bbs-go/server/*.yml ./server/ -COPY ./start.sh ${APP_HOME}/start.sh +COPY --from=site_builder /code/bbs-go/site/.output ./site/.output +COPY --from=site_builder /code/bbs-go/site/node_modules ./site/node_modules +COPY --from=admin_builder /code/bbs-go/admin/dist ./admin +COPY ./start.sh ${APP_HOME}/start.sh RUN chmod +x ${APP_HOME}/start.sh EXPOSE 8082 diff --git a/admin/.dockerignore b/admin/.dockerignore deleted file mode 100644 index d451ff16c..000000000 --- a/admin/.dockerignore +++ /dev/null @@ -1,5 +0,0 @@ -node_modules -.DS_Store -dist -dist-ssr -*.local diff --git a/admin/.env.docker b/admin/.env.docker new file mode 100644 index 000000000..d97ccc414 --- /dev/null +++ b/admin/.env.docker @@ -0,0 +1,2 @@ +VITE_API_BASE_URL= +VITE_API_SITE_URL= diff --git a/admin/Dockerfile b/admin/Dockerfile deleted file mode 100644 index c37887814..000000000 --- a/admin/Dockerfile +++ /dev/null @@ -1,19 +0,0 @@ -FROM node:20 AS builder - -ENV APP_HOME /code/bbs-go/admin -WORKDIR "$APP_HOME" - -COPY . ./ -RUN npm install -g pnpm --registry=https://registry.npmmirror.com -RUN pnpm install --registry=https://registry.npmmirror.com -RUN npm install -g pnpm -RUN pnpm install -RUN pnpm build:docker - -FROM nginx:latest - -COPY --from=builder /code/bbs-go/admin/dist /usr/share/nginx/html - -EXPOSE 80 - -CMD ["nginx", "-g", "daemon off;"] diff --git a/admin/config/vite.config.docker.ts b/admin/config/vite.config.docker.ts index 83b8607f1..b212f56a9 100644 --- a/admin/config/vite.config.docker.ts +++ b/admin/config/vite.config.docker.ts @@ -8,6 +8,7 @@ import configImageminPlugin from './plugin/imagemin'; export default mergeConfig( { mode: 'docker', + base: '/admin', plugins: [ // configCompressPlugin('gzip'), configVisualizerPlugin(), diff --git a/docker-compose.yml b/docker-compose.yml index 20c49b016..5e86b0a3e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -38,3 +38,15 @@ services: BBSGO_DB_URL: root:123456@tcp(bbs-go-mysql:3306)/bbsgo_db?charset=utf8mb4&parseTime=True&loc=Local BBSGO_BASEURL: http://localhost:3000 restart: on-failure + + nginx: + container_name: bbs-go-nginx + image: nginx:latest + ports: + - 80:80 + volumes: + - .docker-compose/nginx.conf:/etc/nginx/conf.d/default.conf # 挂载自定义的 nginx 配置文件 + depends_on: + - bbsgo + restart: on-failure + diff --git a/server/internal/server/server.go b/server/internal/server/server.go index 4bb1fd500..1dd9918ed 100644 --- a/server/internal/server/server.go +++ b/server/internal/server/server.go @@ -51,8 +51,6 @@ func NewServer() { }) }) - app.HandleDir("/admin", "./admin") - // api mvc.Configure(app.Party("/api"), func(m *mvc.Application) { m.Party("/topic").Handle(new(api.TopicController))