Docusaurus Dockerfile 优化7倍体积
未优化的Dockerfile
FROM node:18-alpine
WORKDIR /app
【copy monorepo当中的subpath目录】
COPY /subpath/. /app
RUN npm install && npm run build
EXPOSE 3000
CMD ["npm", "run", "serve"]
已优化的Dockerfile
# ---- Stage 1: Build ----
FROM node:18-alpine AS builder
WORKDIR /app
【我使用的是monorepo,所以把代码都放在subpath】
COPY /subpath/. ./
RUN npm install && npm run build
# ---- Stage 2: Serve ----
FROM nginx:1.28-alpine
RUN rm /etc/nginx/conf.d/default.conf
COPY /subpath/nginx.conf /etc/nginx/nginx.conf
COPY --from=builder /app/build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
【这是从外部的nginx.conf】
user nginx;
worker_processes auto;
worker_rlimit_nofile 8192;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 2048;
multi_accept on;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main buffer=32k flush=5s;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65s;
open_file_cache max=5000 inactive=30s;
open_file_cache_valid 60s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_min_length 256;
gzip_types
text/plain text/css application/json application/javascript
application/x-javascript text/xml application/xml application/xml+rss
text/javascript image/svg+xml;
map $sent_http_content_type $expires {
default off;
text/html epoch;
text/css 1y;
application/javascript 1y;
application/json 1y;
application/xml 1y;
~image/ 1y;
~font/ 1y;
}
server {
listen 80 default_server;
server_name _;
location = /nginx_health {
access_log off;
return 200 "OK";
}
location /subpath/ {
alias /usr/share/nginx/html/;
try_files $uri $uri/ /index.html;
expires $expires; # From map
# --- Corrected Cache-Control logic ---
set $cache_control_value "public"; # Default value
if ($uri ~* "\.[0-9a-f]{8,}\.(css|js|mjs)$") { # Check if asset has a hash in its name
set $cache_control_value "public, max-age=31536000, immutable"; # Override for immutable assets
}
add_header Cache-Control $cache_control_value; # Add the header
# --- End of corrected logic ---
}
location = /subpath {
return 301 /subpath/;
}
}
}
结果:原本175MB减到了23MB

由于我使用的是Monorepo, 所以dockerfile会从subpath去获取代码。
以下是subpath在Kubernetes的部署
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: document-ingress
namespace: document
annotations:
nginx.ingress.kubernetes.io/ssl-redirect: 'true'
spec:
ingressClassName: nginx
rules:
- host: test.pangzai.win
http:
paths:
- backend:
service:
name: document-service
port:
number: 80
path: /subpath
pathType: Prefix
---
apiVersion: v1
kind: Service
metadata:
name: document-service
namespace: document
spec:
selector:
app: document_live
ports:
- name: "80www"
protocol: TCP
port: 80
targetPort: app
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: document-deployment
namespace: document
spec:
replicas: 1
selector:
matchLabels:
app: document_live
template:
metadata:
labels:
app: document_live
spec:
containers:
- name: document-container
image: your_image
imagePullPolicy: Always
ports:
- containerPort: 80
name: app
docusaurus.config.js 的这两个也要改成你的subpath
url: 'https://test.pangzai.win',
baseUrl: '/subpath/'
Facebook评论