3

I am using docker-compose to create a rstudio service which will be integrated with other services such a db. I am trying to pass some environment variables from the docker-compose.yml file to rstudio. I need these variables in the docker-compose file as they will be changing for each customer. I cannot include them in the Dockerfile and also I cannot copy a .Rprofile file to the docker image.

version: '2.4'
services:
  rstudio:
    environment:
      - USER=rstudio
      - PASSWORD=qwerty
      - MY_VAR=1
    image: "rocker/verse:latest"
    ports:
     - 8787:8787

I want to access MY_VAR from the R environment using the rstudio user.

Devaraj Phukan
  • 196
  • 1
  • 9
  • `Sys.getenv()` doesn't work? – Hong Ooi Oct 26 '18 at 10:07
  • 2
    @HongOoi No, Sys.getenv() cannot get MY_VAR from Rstudio window, although if I go inside the container using docker exec, I can access MY_VAR. I want to be able to access from the Rstudio window in the browser. – Devaraj Phukan Oct 26 '18 at 10:10
  • Did you find a solution? I'm in exactly the same situation. running ```docker exec -it container bash``` confirms env's are there, but from RStudio they are not visible. Sys.getenv() nor running env in terminal. – taiyodayo Apr 19 '21 at 07:14

1 Answers1

1

The environmental variables available in Rstudio are coming from the Renviron file located at $R_HOME/etc/Renviron.

According to this thread the environmental variables created when running the docker are not automatically added to this Renviron. Therefore you need to add them yourself at the end of the file for them to show in Rstudio.

You can for instance add the following command at the end of your dockerfile that will copy them from a file on your host machine to the docker. (Assuming $R_HOME=/usr/local/lib/R)

COPY my_file /usr/local/lib/R/etc/
RUN cat /usr/local/lib/R/etc/my_file >> /usr/local/lib/R/etc/Renviron

Where the contents of my_file would be:

MY_VAR=1
Thomas
  • 1,823
  • 1
  • 8
  • 24