diff --git a/Dockerfile b/Dockerfile index 928061d..046c8f5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,73 +16,52 @@ ENV BUILD_TIMESTAMP=${BUILD_TIMESTAMP} RUN echo "Building for environment: ${NODE_ENV}" RUN echo "Build timestamp: ${BUILD_TIMESTAMP}" -# 安装 yarn -RUN apk add --no-cache yarn +# 复制 package.json 和 package-lock.json +COPY package*.json ./ -# 设置 yarn 和 npm 镜像源为淘宝源 -RUN yarn config set registry https://registry.npmmirror.com && \ - npm config set registry https://registry.npmmirror.com +RUN npm config set registry http://mirrors.cloud.tencent.com/npm/ -# 设置工作目录 -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 . . -# 确保babel配置文件存在 -RUN echo '{ "presets": ["@babel/preset-env"] }' > .babelrc - -# 创建或更新环境文件 -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 +# 构建应用 +RUN npm run build # 生产阶段 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 -# 复制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 -# 添加构建信息文件,用于验证部署 -RUN echo "Build completed at: $(date)" > /usr/share/nginx/html/build-info.txt && \ - echo "Environment: ${NODE_ENV}" >> /usr/share/nginx/html/build-info.txt +# 使用非root用户运行 +USER nginx -# 暴露80端口 EXPOSE 80 -# 启动Nginx CMD ["nginx", "-g", "daemon off;"] \ No newline at end of file