以下是我的 docker 檔案中的邏輯。我正在使用 nginx 來構建應用程式。
FROM node:14-alpine as builder
COPY package.json ./
RUN npm install && mkdir /app && mv ./node_modules ./app
WORKDIR /app
COPY . .
RUN npm run build
FROM nginx:1.16.0-alpine
COPY --from=builder /app/build /usr/share/nginx/html
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d
EXPOSE 3000
CMD ["nginx", "-g", "daemon off;"]
下面是 nginx.conf 檔案
server {
listen 3000;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}
location /api {
# rewrite /api/api(.*) /$1 break;
proxy_pass http://${backend_host}:${backend_port}/api;
}
include /etc/nginx/extra-conf.d/*.conf;
}
在使用部署檔案部署映像時,將提供 proxy_pass URL 中的 backend_host 和 backend_port。
這可能嗎?如果沒有,還有其他方法嗎?
uj5u.com熱心網友回復:
如果您想動態掛載nginx.conf,我建議您將配置映射與您的deployment.yaml一起使用
因此,通過這種方式,您可以多次重復使用 docker 映像而不重新創建它,并通過配置映射來更新它。
你的碼頭檔案將是
FROM node:14-alpine as builder
COPY package.json ./
RUN npm install && mkdir /app && mv ./node_modules ./app
WORKDIR /app
COPY . .
RUN npm run build
FROM nginx:1.16.0-alpine
COPY --from=builder /app/build /usr/share/nginx/html
RUN rm /etc/nginx/conf.d/default.conf
EXPOSE 3000
CMD ["nginx", "-g", "daemon off;"]
示例配置圖
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-config
data:
default.conf: |-
server {
listen 80 default_server;
root /var/www/html;
server_name _;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(. \.php)(/. )$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
將配置映射掛載到部署
apiVersion: apps/v1
kind: Deployment
metadata:
name: app
labels:
app: app
spec:
selector:
matchLabels:
app: app
replicas: 1
template:
metadata:
labels:
app: app
spec:
containers:
- name: app
image: app-image
ports:
- containerPort: 80
volumeMounts:
- name: nginx-config
mountPath: /etc/nginx/nginx.conf
subPath: nginx.conf
volumes:
- name: nginx-config
configMap:
name: confnginx
有關更多詳細資訊,請閱讀:https ://blog.meain.io/2020/dynamic-reverse-proxy-kubernetes/
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/472486.html
標籤:码头工人 Kubernetes dockerfile nginx 配置 Kubernetes-部署
上一篇:使用模板創建多個容器
