diff --git a/demo/app/node-frontend-vue2/.dockerignore b/demo/app/node-frontend-vue2/.dockerignore new file mode 100644 index 0000000..89fe1f0 --- /dev/null +++ b/demo/app/node-frontend-vue2/.dockerignore @@ -0,0 +1,4 @@ +dist/ +node_modules/ +.git/ +.DS_Store diff --git a/demo/app/node-frontend-vue2/.gitignore b/demo/app/node-frontend-vue2/.gitignore new file mode 100644 index 0000000..1061ea7 --- /dev/null +++ b/demo/app/node-frontend-vue2/.gitignore @@ -0,0 +1,3 @@ +dist/ +node_modules/ +npm-debug.log* diff --git a/demo/app/node-frontend-vue2/Dockerfile b/demo/app/node-frontend-vue2/Dockerfile new file mode 100644 index 0000000..43a5917 --- /dev/null +++ b/demo/app/node-frontend-vue2/Dockerfile @@ -0,0 +1,24 @@ +# 编译阶段:compile-node22(Vite 7 需要 Node >= 20.19) +FROM registry.cn-shanghai.aliyuncs.com/odboy/kenaito:compile-node22 AS build +WORKDIR /src + +COPY package.json package-lock.json* ./ +# npm 源由镜像的 /usr/local/node/etc/npmrc 指向 npmmirror,无需额外换源 +RUN npm install --no-audit --no-fund + +COPY . ./ +RUN npm run build && ls -lh dist/ dist/assets/ + +# 运行阶段:Tengine 3.1.0(自建 proxy-tengine3 镜像)托管静态文件 +FROM registry.cn-shanghai.aliyuncs.com/odboy/kenaito:proxy-tengine3 AS runtime +LABEL MAINTAINER="tianjun@odboy.cn" +ENV TimeZone=Asia/Shanghai +# 站点配置与构建产物都放进镜像:conf.d/*.conf 由主配置 include,html/ 为站点根目录 +COPY nginx/node-frontend-vue2.conf /usr/local/nginx/conf/conf.d/node-frontend-vue2.conf +COPY --from=build /src/dist/ /usr/local/nginx/html/ +# 构建期自检:配置语法错误直接让镜像构建失败 +# 纯静态站点,配置里没有 proxy_pass 上游主机名,nginx -t 可直接通过 +RUN /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf +EXPOSE 80 +STOPSIGNAL SIGQUIT +CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"] diff --git a/demo/app/node-frontend-vue2/README.md b/demo/app/node-frontend-vue2/README.md new file mode 100644 index 0000000..8ef6257 --- /dev/null +++ b/demo/app/node-frontend-vue2/README.md @@ -0,0 +1,85 @@ +# node-frontend-vue2 示例 + +Vue 2.7 + Element UI 2 的前端项目,**构建阶段**用 `compile-node22` 跑 Vite,**运行阶段**直接以自建 +`proxy-tengine3` 镜像为基座托管静态文件,不再额外起 Node 容器。 + +| 阶段 | 镜像 | 说明 | +| --- | --- | --- | +| 编译 | `registry.cn-shanghai.aliyuncs.com/odboy/kenaito:compile-node22` | Node 22 + npm(源为 npmmirror),Vite 7 要求 Node ≥ 20.19 | +| 运行 | `registry.cn-shanghai.aliyuncs.com/odboy/kenaito:proxy-tengine3` | Tengine 3.1.0,托管 `dist/` | + +纯前端项目:页面数据全部在浏览器内产生,不调用任何后端接口,部署只需要一个 Tengine 容器。 + +### 示例内容 + +``` +node-frontend-vue2/ +├── package.json # vue 2.7.16 / element-ui 2.15.14 / vue-router 3.6.5;vite 7 + plugin-vue2 +├── vite.config.js # base: './';element-ui 单独拆包 +├── index.html +├── src/ +│ ├── main.js # Vue.use(ElementUI) +│ ├── router.js # hash 模式,/ /form /about +│ ├── App.vue # 顶部导航 + router-view +│ └── views/ +│ ├── Home.vue # 计数器、时钟、el-statistic / el-rate / el-progress 等组件一览 +│ ├── Form.vue # el-form 校验、el-select、el-date-picker 等常用组件 +│ └── About.vue # 版本与部署说明 +├── nginx/ +│ └── node-frontend-vue2.conf # Tengine 站点配置:静态托管 + /healthz +├── Dockerfile # 两阶段:compile-node22 -> proxy-tengine3 +├── build.sh # 构建镜像(PUSH=1 时同时推送) +├── docker-compose.yml # 只部署前端(端口 8089) +├── startup.sh / shutdown.sh +└── .dockerignore / .gitignore +``` + +### 构建与部署 + +```shell +sh build.sh # docker build -t registry.cn-shanghai.aliyuncs.com/odboy/kenaito:demo-node-frontend-vue2 . +PUSH=1 sh build.sh # 构建并推送 +sh startup.sh # docker-compose -f docker-compose.yml -p node-frontend-vue2-demo up -d +``` + +访问 http://127.0.0.1:8089/ :首页、表单页、关于页都是纯前端页面,不依赖任何后端容器。 + +```shell +curl http://127.0.0.1:8089/healthz # ok +``` + +### 本地开发(不用 docker) + +```shell +npm install +npm run dev # http://127.0.0.1:5173 +``` + +### 关键点 + +**为什么用 Tengine 托管而不是 Node 托管**:Vue 打包后是纯静态文件,用 `proxy-tengine3` 直接托管, +运行阶段不保留 Node/npm,镜像更小,也能顺手用 Tengine 做 gzip、缓存和反向代理。 + +**站点配置**:配置放在 `nginx/node-frontend-vue2.conf`。构建时文件被 `COPY` 到 `/usr/local/nginx/conf/conf.d/` +(镜像自带的 nginx.conf 已经 `include conf.d/*.conf`);compose 又把宿主机的 `./nginx` 目录挂载到同一个 +`conf.d`,所以改完配置执行 +`docker exec node-frontend-vue2-demo nginx -t && docker exec node-frontend-vue2-demo nginx -s reload` 即生效,不必重建镜像。 +挂目录而不是挂单文件:单文件挂载在宿主机上编辑会换 inode,容器内看不到改动。 +`/assets/` 长缓存(文件带 hash),`/` 用 `try_files $uri $uri/ /index.html` 兜底。 + +**路由模式**:示例用 hash 模式,部署到任何路径都不用额外 rewrite;要换 history 模式,`try_files` 已经写好, +改 `router.js` 的 `mode` 即可。 + +**前后端分离部署**:本示例是纯静态前端,只负责页面渲染,不代理任何接口。实际项目里后端是独立服务、 +配有自己的域名(如 `api.example.com`),由前端直接跨域调用或由统一的网关处理;本仓库不预置任何后端容器, +也不在站点配置里写 `proxy_pass` 上游——这样构建阶段不在业务网络里也能直接 `nginx -t` 通过。 +如果某个部署环境确实需要同域转发,再在那个环境的配置里单独加 `location` 即可,不影响本示例。 + +**依赖版本**:Vue 2.7.16 是 Vue 2 的最后一个版本(内置组合式 API,但示例仍用选项式写法), +`@vitejs/plugin-vue2` 负责让 Vite 7 编译 `.vue` 单文件组件。 + +### 停止 + +```shell +sh shutdown.sh +``` diff --git a/demo/app/node-frontend-vue2/build.sh b/demo/app/node-frontend-vue2/build.sh new file mode 100755 index 0000000..68885e5 --- /dev/null +++ b/demo/app/node-frontend-vue2/build.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash +# 在示例目录下构建示例镜像(编译 + 运行两阶段均在本机 docker 内完成) +set -euo pipefail +cd "$(dirname "$0")" + +IMAGE=${IMAGE:-registry.cn-shanghai.aliyuncs.com/odboy/kenaito:demo-node-frontend-vue2} +docker build -t "${IMAGE}" . + +if [ "${PUSH:-0}" = "1" ]; then + docker push "${IMAGE}" +fi diff --git a/demo/app/node-frontend-vue2/docker-compose.yml b/demo/app/node-frontend-vue2/docker-compose.yml new file mode 100644 index 0000000..ad95958 --- /dev/null +++ b/demo/app/node-frontend-vue2/docker-compose.yml @@ -0,0 +1,32 @@ +version: '3.8' + +# 网段避开其它示例(172.31.0/24 ~ 172.31.2/24 已被 go / rust 占用) +networks: + node-frontend-vue2-demo: + ipam: + driver: default + config: + - subnet: "172.31.3.0/24" + +services: + frontend: + image: registry.cn-shanghai.aliyuncs.com/odboy/kenaito:demo-node-frontend-vue2 # 镜像由 build.sh 构建 + container_name: node-frontend-vue2-demo + restart: unless-stopped + ports: # 映射端口:前端页面 + - "8089:80" + volumes: # 只挂载站点配置目录,改完 reload 即可,不必重建镜像 + # 挂目录而不是单文件:单文件挂载在宿主机上编辑会换 inode,容器内看不到改动 + - "./nginx:/usr/local/nginx/conf/conf.d" + environment: # 设置环境变量,相当于docker run命令中的-e + TZ: Asia/Shanghai + healthcheck: + # 运行镜像自带 net-tools,直接用端口探测 + test: ["CMD-SHELL", "netstat -tln | grep -q ':80 '"] + interval: 30s + timeout: 5s + retries: 3 + start_period: 5s + networks: + node-frontend-vue2-demo: + ipv4_address: 172.31.3.20 diff --git a/demo/app/node-frontend-vue2/index.html b/demo/app/node-frontend-vue2/index.html new file mode 100644 index 0000000..91ccd55 --- /dev/null +++ b/demo/app/node-frontend-vue2/index.html @@ -0,0 +1,12 @@ + + +
+ + +{{ clock }}
+