【问题标题】:Docker-compose variable is not passed into a production vue appDocker-compose 变量未传递到生产 vue 应用程序中
【发布时间】:2020-06-13 21:29:26
【问题描述】:

Docker-compose 环境变量似乎根本没有设置。我曾尝试使用 env_file 和 environment 字段,但是在我的 vue 应用程序中打印 process.env 时,唯一可见的变量是 NODE_ENV 和 BASE_URL

这是我的 docker-compose 代码:

    frontend:
    container_name: "Frontend"
    build:
        context: .
        dockerfile: frontend.dockerfile
    env_file:
        - ./frontend.env
    environment: 
        VUE_APP_BACKEND_URL: "django"
    ports: 
        - 8000:80
    command: echo $VUE_APP_BACKEND_URL

前端 Dockerfile:

# build stage
FROM node:lts-alpine as build-stage
WORKDIR /app
COPY ./front/package*.json ./
RUN npm install
COPY ./front .
RUN npm run build

# production stage
FROM nginx:stable-alpine as production-stage
COPY --from=build-stage /app/dist /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

我的 frontend.env 代码(这是在我尝试调试问题时添加的):

VUE_APP_BACKEND_URL=django
VUE_APP_BACKEND_PORT=9000

这是我的js代码

export function getEnvironmentVar(key,defaultVal){
  window.console.log(process.env)
  window.console.log(process.env.VUE_APP_BACKEND_URL)
  var result = process.env[`VUE_APP_${key}`];
  window.console.log(`Trying to read environment variable: ${key}, got: ${result}`)
  if(result!= undefined)
    return result
  else 
    return defaultVal
}

输出:

{NODE_ENV: "production", BASE_URL: "/"}
NODE_ENV: "production"
BASE_URL: "/"
__proto__: Object

这是在 docker-compose 中添加了命令行的输出:

WARNING: The VUE_APP_BACKEND_URL variable is not set. Defaulting to a blank string.
Starting Frontend ... done
Attaching to Frontend
Frontend              | 
Frontend exited with code 0

我做错了什么?

【问题讨论】:

  • 可以添加docker文件吗?
  • Dockerfile 已添加到问题中

标签: node.js docker vue.js docker-compose


【解决方案1】:

您设置的环境变量应用于容器的运行时环境,而不是在构建时应用于容器。由于您的 Web 应用程序是静态构建和提供的,因此在构建应用程序时未设置环境变量,因此在前端将不可用。

要让vue-cli 在构建您的应用时看到环境变量,您需要在Dockerfile 中使用构建参数。然后,您可以在运行 npm run build 之前,在构建阶段使用与构建参数相同的值设置环境变量。

frontend.dockerfile

# build stage
FROM node:lts-alpine as build-stage
WORKDIR /app
ARG VUE_APP_BACKEND_URL                       # <-- these two lines have
ENV VUE_APP_BACKEND_URL=$VUE_APP_BACKEND_URL  #      been added
COPY ./front/package*.json ./
RUN npm install
COPY ./front .
RUN npm run build

# production stage
FROM nginx:stable-alpine as production-stage
COPY --from=build-stage /app/dist /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

docker-compose.yml

services:
  frontend:
    container_name: "Frontend"
      build:
        context: .
        dockerfile: frontend.dockerfile
        args:
          - VUE_APP_BACKEND_URL=django
    ports: 
      - 8000:80

由于在执行npm run build 之前设置了VUE_APP_BACKEND_URL,因此环境变量将嵌入到您构建的应用程序中。

来源:

【讨论】:

    【解决方案2】:

    你可以试试 dotenv 包

    npm i dotenv
    

    添加到文件顶部

    require('dotenv').config()
    

    【讨论】:

    • 添加 dotenv 似乎没有任何效果。变量仍然不存在
    猜你喜欢
    • 2019-12-26
    • 2019-06-18
    • 1970-01-01
    • 2020-02-23
    • 2017-09-18
    • 2023-01-30
    • 2020-04-30
    • 1970-01-01
    相关资源
    最近更新 更多