8

I have a running Docker container with the shiny server from the slightly modified rocker/shiny image.

The default shiny-server.conf file sets the shiny user as the one under

# Define the user we should use when spawning R Shiny processes
run_as shiny;

means the server is running as root by default, but the worker processes for shiny apps are run as user shiny

The apps themselves use a data warehouse connection to the SQL server, initialized via RODBC. While we did not want to put the entire connection details string (including DB host and password) into the codebase, we wanted to read them from the environment variables with which the container is created by running the following routine

HOST <- Sys.getenv("host")
DB <- Sys.getenv("db")
UID <- Sys.getenv("uid")
PWD <- Sys.getenv("pwd")

conn<-paste0("driver={ODBC Driver 17 for SQL Server};server=",HOST,";database=",DB,";uid=",UID,";pwd=",PWD)
dbhandle<-odbcDriverConnect(conn)

The problem is, that those env variables are empty when the worker process is spawned within a container as the user shiny.

If I try to run the same code in the interactive R console (as both root, or shiny user) I am getting the env variables as expected.

Any input would be much appreciated. Please note I do not intend to use docker secrets as I am not running the app within a docker swarm cluster, just a standalone Rancher OS host.

EDIT: While .Renviron file might be a viable alternative to solving that particular problem, it would entail putting the variables into the codebase, which we are trying to avoid here.

Michal Paczes
  • 199
  • 11
  • Maybe you didn't pass ENV in container starting or didn't define the process which needs ENV as ENTRYPOINT nor CMD – Alejandro Galera Jun 28 '18 at 10:46
  • I did pass the --env-file on `docker run` that is correctly interpreted - I am running *printenv* in bash within the container as both users *shiny* and *root*, getting the variables returned to me as expected. – Michal Paczes Jun 28 '18 at 10:53
  • 1
    Possible duplicate of [How to pass environment variables to shinyapps](https://stackoverflow.com/questions/39084284/how-to-pass-environment-variables-to-shinyapps) – Ralf Stubner Jun 28 '18 at 11:03
  • You could use an `entrypoint` that reads the necessary variables from then environment and writes them to `.Renviron`. – Ralf Stubner Jun 28 '18 at 12:19

1 Answers1

10

I added the following in shiny-server.sh start script which is the docker container's CMD, as suggested by Ralf Stubner

env > /home/shiny/.Renviron
chown shiny.shiny /home/shiny/.Renviron
Michal Paczes
  • 199
  • 11