[英]React process.env can't read environment variables in docker-compose.yml
My React project use environment variables and there the variables in docker-compose.yml.我的 React 项目使用环境变量和 docker-compose.yml 中的变量。 but they couldn't be read from the project.
但无法从项目中读取它们。
docker-compose.yml docker-compose.yml
version: '3.4'
services:
frontend:
build:
context: .
target: app
environment:
- API_PROTOCOL=http
- API_HOST=localhost
- API_PORT=10000
volumes:
- app-volume:/share
command: cp -R ./build /share
...
/* Ngnix Setting */
...
volumes:
app-volume:
api.js api.js
export const API_PROTOCOL = process.env.API_PROTOCOL || 'http';
export const API_HOST = process.env.API_HOST || 'localhost';
export const API_PORT = process.env.API_PORT || '8080';
export const getAPIUrl = () => {
return `${API_PROTOCOL}://${API_HOST}:${API_PORT}`
};
And I printed using console.log(getAPIUrl());
我使用
console.log(getAPIUrl());
打印. .
but it shows up default values like below.但它显示如下默认值。
Output Output
http://localhost:8080
You must create custom environment variables beginning with REACT_APP_
.您必须创建以
REACT_APP_
开头的自定义环境变量。 Any other variables except NODE_ENV will be ignored to avoid accidentally exposing a private key on the machine that could have the same name.除了 NODE_ENV 之外的任何其他变量都将被忽略,以避免意外暴露可能具有相同名称的机器上的私钥。 Changing any environment variables will require you to restart the development server if it is running.
更改任何环境变量都需要您重新启动正在运行的开发服务器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.