简体   繁体   English

"如何将所有 Linux 环境变量读入 Docker 容器中的 R?"

[英]How to read all Linux environment variables into R in a Docker container?

I am trying to make an R-Shiny web app secure be getting all of my db credentials out of the code.我正在尝试使 R-Shiny Web 应用程序安全地从代码中获取我的所有数据库凭据。 I am using Azure Webapps for containers and in run time I set environment variables securily via Azure Keyvault.我将 Azure Webapps 用于容器,在运行时我通过 Azure Keyvault 安全地设置环境变量。

However, when I use Sys.getenv()但是,当我使用 Sys.getenv()

Sys.getenv("AzureSQL_Prod") Sys.getenv("AzureSQL_Prod")

... the specified environment variable does not appear. ...没有出现指定的环境变量。 So the questions is why?那么问题是为什么? And how do I get R to understand that I need to access this environment variable?以及如何让 R 了解我需要访问此环境变量?

R environment variables are generated from the .Renviron file - see this Rstudio link for more details ( https://support.rstudio.com/hc/en-us/articles/360047157094-Managing-R-with-Rprofile-Renviron-Rprofile-site-Renviron-site-rsession-conf-and-repos-conf ). R 环境变量是从 .Renviron 文件生成的 - 有关更多详细信息,请参阅此 Rstudio 链接( https://support.rstudio.com/hc/en-us/articles/360047157094-Managing-R-with-Rprofile-Renviron-Rprofile -site-Renviron-site-rsession-conf-and-repos-conf )。

When you invoke Sys.getenv() , the R process searches for R environment variables - not system environment variables.当您调用Sys.getenv()时,R 进程会搜索 R 环境变量,而不是系统环境变量。

One option to get around this is to create a .Renviron file - see the link above for details.解决此问题的一种方法是创建一个.Renviron文件 - 有关详细信息,请参阅上面的链接。

An alternative is to retrieve system environment variables explicitly from your R code.另一种方法是从 R 代码中显式检索系统环境变量。 My approach is as follows:我的方法如下:

system("printenv MY_ENV_VARIABLE", intern = TRUE)

This should output the string corresponding to your system variable.这应该输出与您的系统变量对应的字符串。 The intern = TRUE allows you to save the output as a variable in your R environment. intern = TRUE允许您将输出保存为 R 环境中的变量。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM