rocker-org / rocker-versioned2

Run current & prior versions of R using docker. rocker/r-ver, rocker/rstudio, rocker/shiny, rocker/tidyverse, and so on.
https://rocker-project.org
GNU General Public License v2.0
409 stars 174 forks source link

Ability to preserve environment variables #93

Closed robkooper closed 3 years ago

robkooper commented 3 years ago

In the PEcAN project we leverage of these docker images for our R and RStudio needs. We install additional packages, and will start RStudio. Some our code depends on environment variables that we inject into the container, for example we pass in the URI for RabbitMQ as an environment variable. Unfortunately it seems that somewhere these are removed and when in the RStudio we do Sys.getenv("RABBITMQ_URI") it will return "".

I tried setting S6_KEEP_ENV=1 but that did not work. Right now I'm using the following solution that works:

Use an environment variable called KEEP_ENV that lists all environment variables to preserve for RStudio, and the following script:

#!/bin/bash

mkdir -p /home/${USER}
#if [ ! -e "/home/${USER}/.Renviron" ]; then
     echo "# Environment Variables" >> "/home/${USER}/.Renviron"
#fi

for x in ${KEEP_ENV}; do
    value="$(echo "${!x}" | sed 's/\//\\\//g')"
    sed -i -e "/^$x=/{h;s/=.*/=${value}/};\${x;/^\$/{s//$x=${value}/;H};x}" "/home/${USER}/.Renviron"
done

exec /init

Let me know if there is an easier way to enable me to store environment variables for use in RStudio.

eddelbuettel commented 3 years ago

@cboettig has riffed on this before---apparently pruning the existing environment is simply what RStudio does :-/

cboettig commented 3 years ago

Hey @robkooper ,

yup, like Dirk says, RStudio decided that the server should pay no attention to the system env vars. Could be a security thing (note we do put PASSWORD in an env var, which at least in a multi-user setting you may not want to expose to the R session) but iirc it is just because it gives them a more consistent environment to troubleshoot. As you discovered, you'll have to write any env var you want to expose to RStudio into an Renviron. We usually append to $R_HOME/etc/Renviron in our dockerfiles, but writing to the user's .Renviron is fine too.

of course anything running in cli R instead of RStudio doesn't have this issue.