我有一个 vue 应用程序在前端运行,而 spring boot 后端则在不同的容器上运行。 我想对我的 vuejs 应用程序进行 dockerize,以将环境变量从 docker-compose 文件传递到 nginx。
我的问题是我的 nginx conf 文件没有从 docker-compose 获取环境变量。
Docker 撰写文件
backend-service:
container_name: backend-service
image: backend-service-local
networks:
- app-network
ports:
- 8081:8080
restart: on-failure
depends_on:
postgresdb:
condition: service_healthy
vue-app:
container_name: vue-app
image: vue-app-local
networks:
- app-network
ports:
- 8080:80
environment:
VUE_APP_BASE_URL: http://backend-service:8080
restart: on-failure
depends_on:
backend-service:
condition: service_started
Docker 文件
# build stage
FROM node:lts-alpine as build-stage
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
# production stage
FROM nginx:stable-alpine as production-stage
COPY --from=build-stage /app/dist /usr/share/nginx/html
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
NGINX 配置
# Run as a less privileged user for security reasons.
user nginx;
# #worker_threads to run;
# "auto" sets it to the #CPU_cores available in the system, and
# offers the best performance.
worker_processes auto;
events { worker_connections 1024; }
http {
sendfile on;
upstream docker-backend {
server ${VUE_APP_BASE_URL};
}
server {
# Hide nginx version information.
server_tokens off;
listen 80;
root /usr/share/nginx/html;
include /etc/nginx/mime.types;
location / {
try_files $uri $uri/ /index.html;
}
location /api/ {
proxy_pass http://docker-backend;
}
}
}
最佳答案
请告知nginx docker image docs在页面的在 nginx 配置中使用环境变量
部分。
nginx docker 镜像处理环境变量的方式是使用链接页面中的配置在运行时注入(inject)它们
关于Docker compose 环境变量未在 nginx 中获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69113482/