This commit is contained in:
aaron 2025-03-12 08:25:45 +08:00
parent b930764795
commit a054f5052c

View File

@ -16,73 +16,52 @@ 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}"
# 安装 yarn # 复制 package.json 和 package-lock.json
RUN apk add --no-cache yarn COPY package*.json ./
# 设置 yarn 和 npm 镜像源为淘宝源 RUN npm config set registry http://mirrors.cloud.tencent.com/npm/
RUN yarn config set registry https://registry.npmmirror.com && \
npm config set registry https://registry.npmmirror.com
# 设置工作目录 # 安装依赖
WORKDIR /app RUN npm install --production=false
# 复制 package.json 和 yarn.lock # 复制源代码
COPY package.json yarn.lock* ./
# 安装所有依赖,包括开发依赖
RUN yarn install --frozen-lockfile --production=false && \
yarn add dotenv@16.3.1 --exact && \
echo "Installed dependencies:" && \
yarn list --depth=0 | grep -E '@babel|webpack|vue'
# 复制项目文件
COPY . . COPY . .
# 确保babel配置文件存在 # 构建应用
RUN echo '{ "presets": ["@babel/preset-env"] }' > .babelrc RUN npm run build
# 创建或更新环境文件
RUN echo "NODE_ENV=${NODE_ENV}" > .env && \
echo "BUILD_TIMESTAMP=${BUILD_TIMESTAMP}" >> .env && \
if [ "$NODE_ENV" = "production" ]; then \
echo "VUE_APP_API_URL=https://api.beefast.co" >> .env; \
elif [ "$NODE_ENV" = "testing" ]; then \
echo "VUE_APP_API_URL=https://api-dev.beefast.co" >> .env; \
else \
echo "VUE_APP_API_URL=http://localhost:8000" >> .env; \
fi && \
cat .env
# 根据环境变量选择构建命令
RUN if [ "$NODE_ENV" = "production" ]; then \
echo "Running production build" && \
npx --yes webpack --mode production; \
elif [ "$NODE_ENV" = "testing" ]; then \
echo "Running testing build" && \
npx --yes webpack --mode development --env testing; \
else \
echo "Running development build" && \
npx --yes webpack --mode development; \
fi
# 生产阶段 # 生产阶段
FROM nginx:stable-alpine as production-stage FROM nginx:stable-alpine as production-stage
# 切换 Alpine 镜像源为阿里云 # 安装基础工具
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories RUN apk add --no-cache bash curl
# 复制构建结果到Nginx目录 # 创建必要的目录并设置权限
RUN mkdir -p /var/cache/nginx/client_temp \
/var/cache/nginx/proxy_temp \
/var/cache/nginx/fastcgi_temp \
/var/cache/nginx/uwsgi_temp \
/var/cache/nginx/scgi_temp \
&& chmod 700 /var/cache/nginx/* \
&& chown -R nginx:nginx /var/cache/nginx
# 复制 nginx 配置
COPY nginx.conf /etc/nginx/nginx.conf
# 从构建阶段复制构建结果
COPY --from=build-stage /app/dist /usr/share/nginx/html COPY --from=build-stage /app/dist /usr/share/nginx/html
# 复制Nginx配置文件 # 修改目录权限
COPY nginx.conf /etc/nginx/conf.d/default.conf RUN chown -R nginx:nginx /usr/share/nginx/html \
&& chmod -R 755 /usr/share/nginx/html \
&& chown -R nginx:nginx /var/log/nginx \
&& chmod -R 755 /var/log/nginx \
&& touch /var/run/nginx.pid \
&& chown -R nginx:nginx /var/run/nginx.pid
# 添加构建信息文件,用于验证部署 # 使用非root用户运行
RUN echo "Build completed at: $(date)" > /usr/share/nginx/html/build-info.txt && \ USER nginx
echo "Environment: ${NODE_ENV}" >> /usr/share/nginx/html/build-info.txt
# 暴露80端口
EXPOSE 80 EXPOSE 80
# 启动Nginx
CMD ["nginx", "-g", "daemon off;"] CMD ["nginx", "-g", "daemon off;"]