【发布时间】: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