add dockerfile

This commit is contained in:
LittleBoy 2024-05-22 15:58:36 +08:00
parent abb7b4c613
commit 493f15f848
3 changed files with 73 additions and 0 deletions

39
Dockerfile Normal file
View File

@ -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;"]

4
netlify.toml Normal file
View File

@ -0,0 +1,4 @@
[[redirects]]
from = "/*"
to = "/index.html"
status = 200

30
nginx.conf Normal file
View File

@ -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;
}
}