Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preserve environment variables when spawning shiny processes within a container

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.

like image 390
Michal Paczes Avatar asked Jun 28 '18 10:06

Michal Paczes


1 Answers

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
like image 80
Michal Paczes Avatar answered Sep 28 '22 03:09

Michal Paczes