This commit is contained in:
aaron 2025-03-12 09:43:22 +08:00
parent cc98e5dccb
commit 110827a517
2 changed files with 67 additions and 83 deletions

View File

@ -4,6 +4,7 @@ FROM node:18-alpine as build-stage
# 切换 Alpine 镜像源为阿里云 # 切换 Alpine 镜像源为阿里云
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
# 设置工作目录
WORKDIR /app WORKDIR /app
# 设置环境变量 # 设置环境变量
@ -18,17 +19,25 @@ ENV BUILD_TIMESTAMP=${BUILD_TIMESTAMP}
RUN echo "Building for environment: ${NODE_ENV}" RUN echo "Building for environment: ${NODE_ENV}"
RUN echo "Build timestamp: ${BUILD_TIMESTAMP}" RUN echo "Build timestamp: ${BUILD_TIMESTAMP}"
# 复制 package.json 和 package-lock.json # 复制依赖文件
COPY package*.json ./ COPY package.json package-lock.json* ./
RUN npm config set registry http://mirrors.cloud.tencent.com/npm/ RUN npm config set registry http://mirrors.cloud.tencent.com/npm/
# 安装依赖 # 安装依赖
RUN npm install --production=false RUN npm ci && \
echo "Installed dependencies:" && \
npm list --depth=0 | grep -E '@babel|webpack|vue'
# 复制源代码 # 确保babel配置文件存在
RUN echo '{ "presets": ["@babel/preset-env"] }' > .babelrc
# 复制项目文件
COPY . . COPY . .
# 创建或更新环境文件
RUN echo "VUE_APP_API_URL=${API_URL:-/api}" > .env.production
# 构建应用 # 构建应用
RUN npm run build RUN npm run build
@ -39,32 +48,32 @@ FROM nginx:stable-alpine as production-stage
RUN apk add --no-cache bash curl RUN apk add --no-cache bash curl
# 创建必要的目录并设置权限 # 创建必要的目录并设置权限
RUN mkdir -p /var/cache/nginx/client_temp \ RUN mkdir -p /var/cache/nginx/client_temp && \
/var/cache/nginx/proxy_temp \ mkdir -p /var/cache/nginx/proxy_temp && \
/var/cache/nginx/fastcgi_temp \ mkdir -p /var/cache/nginx/fastcgi_temp && \
/var/cache/nginx/uwsgi_temp \ mkdir -p /var/cache/nginx/uwsgi_temp && \
/var/cache/nginx/scgi_temp \ mkdir -p /var/cache/nginx/scgi_temp && \
&& chmod 700 /var/cache/nginx/* \ mkdir -p /var/run && \
&& chown -R nginx:nginx /var/cache/nginx mkdir -p /usr/share/nginx/html && \
chmod -R 755 /var/cache/nginx /var/run /usr/share/nginx/html
# 复制 nginx 配置 # 删除默认配置
RUN rm -f /etc/nginx/conf.d/default.conf
# 复制自定义nginx配置
COPY nginx.conf /etc/nginx/conf.d/default.conf COPY nginx.conf /etc/nginx/conf.d/default.conf
# 从构建阶段复制构建结果 # 复制构建文件
COPY --from=build-stage /app/dist /usr/share/nginx/html COPY --from=build-stage /app/dist /usr/share/nginx/html
# 修改目录权限 # 设置权限
RUN chown -R nginx:nginx /usr/share/nginx/html \ RUN chmod -R 755 /usr/share/nginx/html && \
&& chmod -R 755 /usr/share/nginx/html \ touch /var/run/nginx.pid && \
&& chown -R nginx:nginx /var/log/nginx \ chmod 644 /var/run/nginx.pid && \
&& chmod -R 755 /var/log/nginx \ chmod -R 755 /etc/nginx/conf.d
&& touch /var/run/nginx.pid \
&& chown -R nginx:nginx /var/run/nginx.pid \
&& chmod -R 755 /etc/nginx/conf.d
# 使用root用户运行nginx标准做法
# USER nginx
# 暴露端口
EXPOSE 80 EXPOSE 80
# 启动nginx
CMD ["nginx", "-g", "daemon off;"] CMD ["nginx", "-g", "daemon off;"]

View File

@ -1,33 +1,9 @@
# 使用标准的nginx配置结构 server {
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
gzip on;
server {
listen 80; listen 80;
server_name localhost; server_name localhost;
# 添加 gzip 压缩 # 添加 gzip 压缩
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
# 设置客户端最大body大小 # 设置客户端最大body大小
@ -62,5 +38,4 @@ http {
location = /50x.html { location = /50x.html {
root /usr/share/nginx/html; root /usr/share/nginx/html;
} }
}
} }