From 18fe9e61b17870129cd3cc1529be19869dafb651 Mon Sep 17 00:00:00 2001 From: huang Date: Sat, 11 Apr 2026 15:29:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20Docker=20=E9=83=A8?= =?UTF-8?q?=E7=BD=B2=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Sisyphus --- Dockerfile | 60 +++++++++++++++++++++++++++++++++++++++++ docker-compose.prod.yml | 26 ++++++++++++++++++ docker/nginx.conf | 31 +++++++++++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 Dockerfile create mode 100644 docker-compose.prod.yml create mode 100644 docker/nginx.conf diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..ac61335 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,60 @@ +# ================================ +# 阶段 1: 构建阶段 +# ================================ +FROM --platform=linux/amd64 node:20-alpine AS builder + +# 使用阿里云镜像源加速 +RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories + +# 设置工作目录 +WORKDIR /build + +# 设置 npm 镜像源 +RUN npm config set registry https://registry.npmmirror.com + +# 复制 package.json(利用 Docker 缓存) +COPY package.json ./ + +# 使用 uvm 自动安装最新兼容的 @dcloudio 包版本 +RUN npx @dcloudio/uvm@latest + +# 安装所有依赖 +RUN npm install --legacy-peer-deps + +# 复制源代码 +COPY . . + +# 构建 H5 生产版本 +RUN npm run build:h5 + +# ================================ +# 阶段 2: 运行阶段 +# ================================ +FROM --platform=linux/amd64 nginx:alpine + +# 使用阿里云镜像源加速 +RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories + +# 设置时区 +RUN apk add --no-cache tzdata && \ + cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \ + echo "Asia/Shanghai" > /etc/timezone + +# 删除默认 nginx 配置 +RUN rm -rf /etc/nginx/conf.d/default.conf + +# 复制自定义 nginx 配置 +COPY docker/nginx.conf /etc/nginx/conf.d/default.conf + +# 从构建阶段复制构建产物 +COPY --from=builder /build/dist/build/h5 /usr/share/nginx/html + +# 暴露端口 +EXPOSE 80 + +# 健康检查 +HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \ + CMD wget --no-verbose --tries=1 --spider http://127.0.0.1:80/health || exit 1 + +# 启动 nginx +CMD ["nginx", "-g", "daemon off;"] diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml new file mode 100644 index 0000000..feaff02 --- /dev/null +++ b/docker-compose.prod.yml @@ -0,0 +1,26 @@ +version: '3.8' + +services: + web: + image: registry.boss160.cn/junhong/device-voice-h5:latest + container_name: junhong-device-voice-h5 + restart: unless-stopped + ports: + - '3003:80' + networks: + - device-voice-network + healthcheck: + test: ['CMD', 'wget', '--no-verbose', '--tries=1', '--spider', 'http://127.0.0.1:80/health'] + interval: 30s + timeout: 3s + retries: 3 + start_period: 5s + logging: + driver: 'json-file' + options: + max-size: '10m' + max-file: '3' + +networks: + device-voice-network: + driver: bridge diff --git a/docker/nginx.conf b/docker/nginx.conf new file mode 100644 index 0000000..94468c2 --- /dev/null +++ b/docker/nginx.conf @@ -0,0 +1,31 @@ +server { + listen 80; + server_name localhost; + root /usr/share/nginx/html; + index index.html; + + gzip on; + gzip_vary on; + gzip_min_length 1024; + gzip_proxied any; + gzip_types text/plain text/css text/xml text/javascript application/javascript application/json application/xml; + gzip_comp_level 6; + + location /health { + access_log off; + return 200 'OK'; + add_header Content-Type text/plain; + } + + location / { + try_files $uri $uri/ /index.html; + } + + location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ { + expires 1y; + add_header Cache-Control "public, immutable"; + access_log off; + } + + error_page 404 /index.html; +}