# 设置基础镜像
FROM node:21.7.3-alpine3.20 as build-stage
# 定义作者
LABEL maintainer=Zenas

#创建一个工作目录
WORKDIR /app

# 复制package.json文件和package-lock.json文件到容器工作目录
COPY . /app

# 安装项目依赖
RUN yarn install

# 构建前端项目
RUN yarn build

# production stage
FROM nginx as production-stage
#COPY --from=build-stage ./dist /usr/share/nginx/html

RUN echo "server {  \
                      listen       80; \
                      location   /shop-api/ { \
                      proxy_pass              http://69.230.201.115:8080/shop-api/; \
                      proxy_redirect          off; \
                      proxy_set_header        Host http://69.230.201.115:8080; \
                      proxy_set_header        X-Real-IP \$remote_addr; \
                      proxy_set_header        X-Forwarded-For \$proxy_add_x_forwarded_for; \
                  } \
                  #解决Router(mode: 'history')模式下,刷新路由地址不能找到页面的问题 \
                  location / { \
                     root   /var/www/html/; \
                      index  index.html index.htm; \
                      if (!-e \$request_filename) { \
                          rewrite ^(.*)\$ /index.html?s=\$1 last; \
                          break; \
                      } \
                  } \
                  access_log  /var/log/nginx/access.log ; \
              } " > /etc/nginx/conf.d/default.conf \
    &&  mkdir  -p  /var/www \
    &&  mkdir -p /var/www/html

ADD /app/dist/ /var/www/html/
# 暴露镜像端口
EXPOSE 80
EXPOSE 443
CMD ["nginx","-g","daemon off"]