From 493f15f848db847381728749b55646e253b965c7 Mon Sep 17 00:00:00 2001 From: callmeyan Date: Wed, 22 May 2024 15:58:36 +0800 Subject: [PATCH] :sparkles: add dockerfile --- Dockerfile | 39 +++++++++++++++++++++++++++++++++++++++ netlify.toml | 4 ++++ nginx.conf | 30 ++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 Dockerfile create mode 100644 netlify.toml create mode 100644 nginx.conf diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..2efe614 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,39 @@ +# Dockerfile for a React app build and run + +FROM node:18.19.1-alpine AS builder +MAINTAINER yaclty2@gmail.com +WORKDIR /app + +# Copy source code to the builder +COPY package.json yarn.lock* ./ +COPY public ./public +COPY src ./src +COPY index.html ./ +COPY *.ts . +COPY *.json . + +# Install dependencies and build the app +RUN npm config set registry https://registry.npmmirror.com +RUN yarn install +RUN yarn build + + +# Step 2. Production image, copy all the files and run nginx +FROM nginx:1.26-alpine3.19 AS runner + +WORKDIR /app + +# envs 配置 +ENV APP_API_URL https://baidu.com + +# nginx配置文件 +COPY nginx.conf /etc/nginx/conf.d/default.conf.template +# 编译文件 +COPY --from=builder /app/dist ./ +# RUN /bin/sh envsubst /etc/nginx/templates/*.template /etc/nginx/conf.d +WORKDIR /etc/nginx/conf.d/ +ENTRYPOINT envsubst '$APP_API_URL' < default.conf.template > default.conf && cat default.conf && nginx -g 'daemon off;' +# 暴露80端口 +EXPOSE 80 +# 启动Nginx服务 +# CMD ["nginx", "-g", "daemon off;"] \ No newline at end of file diff --git a/netlify.toml b/netlify.toml new file mode 100644 index 0000000..b87b8d3 --- /dev/null +++ b/netlify.toml @@ -0,0 +1,4 @@ +[[redirects]] + from = "/*" + to = "/index.html" + status = 200 diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..07c3139 --- /dev/null +++ b/nginx.conf @@ -0,0 +1,30 @@ +server { + listen 80; + listen [::]:80; + server_name localhost; + + #access_log /var/log/nginx/host.access.log main; + + location / { + root /app; + if (!-e $request_filename) { + rewrite ^(.*)$ /index.html last; + break; + } + } + + location ^~/api { + proxy_pass $APP_API_URL; + 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 REMOTE-HOST $remote_addr; + proxy_set_header Upgrade $http_upgrade; + proxy_http_version 1.1; + proxy_ssl_verify off; + # proxy_hide_header Upgrade; + + add_header X-Cache $upstream_cache_status; + } +} +