一、前端Dockerfile
# 使用官方 Node.js 镜像作为基础镜像,当前使用22版本
FROM node:22-alpine
# 设置工作目录
WORKDIR /app
# 复制 package.json 和 package-lock.json
COPY package*.json ./
# 镜像源修改
RUN npm install pnpm -g
RUN pnpm config set registry https://registry.npmmirror.com
# 安装依赖
RUN pnpm install
# 复制项目文件
COPY . .
# 构建应用
RUN pnpm run build
FROM nginx:alpine
# 设置时区环境变量并配置 tzdata
ENV TZ=Asia/Shanghai
RUN ln -fs /usr/share/zoneinfo/$TZ /etc/localtime && \
echo $TZ > /etc/timezone
# 复制构建产物到 Nginx 的 html 目录
COPY --from=0 /app/dist /usr/share/nginx/html
# 复制自定义的 Nginx 配置(可选)
COPY nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
对应nginx.confg
server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location /api {
rewrite ^/api(.*)$ $1 break;
proxy_pass http://192.168.1.99:8000;
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;
# 连接优化参数
proxy_http_version 1.1;
proxy_set_header Connection "";
# 超时控制
proxy_connect_timeout 500ms;
proxy_read_timeout 3s;
proxy_send_timeout 2s;
# 容错配置
proxy_next_upstream error timeout;
proxy_next_upstream_timeout 1s;
proxy_next_upstream_tries 3;
}
}
二、后端Dockerfile
FROM openjdk:21-jdk
# 设置时区环境变量并配置 tzdata
ENV TZ=Asia/Shanghai
RUN ln -fs /usr/share/zoneinfo/$TZ /etc/localtime && \
echo $TZ > /etc/timezone
WORKDIR /app
COPY target/xxxx-server-0.0.1-SNAPSHOT.jar /app/app.jar
EXPOSE 8080
CMD ["java", "-jar", "app.jar"]
专注JAVA系统优化、系统结构调整、系统问题排查医治、系统升级、架构设计、SQL语句优化、小程序、APP、企业应用软件开发,请 + hekf888,欢迎关注,时常发布技术分享博文