简体   繁体   English

如何在js脚本中读取docker环境变量?

[英]How do I read a docker environment variable within a js script?

I have a react app hosted on a node docker container. 我在节点docker容器上托管了一个React应用程序。 I run it and pass it an environment variable 我运行它并将其传递给环境变量

 docker run -t -e "ADMIN_HOST_ENV=http://myapp.app:443/api" -p 3000:3000 "myapp"

How can I get that "ADMIN_HOST_ENV" environment variable within the app? 如何在应用程序中获取该“ ADMIN_HOST_ENV”环境变量?

If this is just about the node backend, there's already a question with a answer: Read environment variables in Node.js 如果这仅与节点后端有关,那么已经有一个问题和答案: 在Node.js中读取环境变量

On the frontend, you'll have several options to get at it, create an endpoint to get it, write it to a .js file that's referenced by the frontend, pass it up as a cookie, keep it backend side and only reference it when you hit the backend, etc. 在前端,您将有多种选择来实现它,创建一个端点来获取它,将其写入前端引用的.js文件,将其作为cookie传递,保留在后端,仅引用它当您点击后端时,等等。

I check environment variables when I run my server, write them to an environment.js, and then reference that environment.js in my index.html. 我在运行服务器时检查环境变量,将其写入environment.js,然后在index.html中引用该environment.js。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将环境变量转换为 JS 中的对象? - How do I convert an environment variable to an object in JS? 如何在docker文件中获取对我可用的docker环境变量? (使用React / JS / webpack) - How do I get the docker environment variables available to me in the docker file? (using React/JS/webpack) 如何在JavaScript脚本中循环php变量 - how do I loop a php variable within a javascript script 如何从 map 中的 promise 设置 JS 变量? - How do I set JS variable from promise within map? 如何从脚本标记中读取JS作为数据? - How do I read JS as data from a script tag? 如何从另一个 Node.js 脚本中运行 Node.js 脚本 - How do I run a Node.js script from within another Node.js script 如何将变量传递给javascript文件<script src=“../js/myfilename.js”></script> - How do I pass a variable to a javascript file <script src=“../js/myfilename.js”></script> 如何在 interperter 中擦除 Rhino 中的 JS 环境? - How can I wipe the JS environment in Rhino within the interperter? 我如何在nodejs中读取操作系统环境变量 - How can i read Operarting System Environment variable in nodejs 如何从 html 中的内联 js 脚本获取变量并将其放入外部 .js 文件中? - How do i get a variable from an inline js script in html and put it into an external .js file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM