0

Problem: I containerized a shiny application with Shiny server pro and docker. Similar to here: https://support.rstudio.com/hc/en-us/articles/360009986893-Shiny-Server-Pro-with-Docker

Question: How can I pass environment variables during run-time to the docker container such that those environment variables can be used in the app (example app below).

The docker command used is: docker run -it --rm -p 3838:3838 -e env_variable_interest=testname <shiny_image>, i.e. the variable env_variable_interest is not shown/recognized/transferred in Shiny server.

Many thanks for comments!!

library(shiny)

ui <- fluidPage(

    # Application title
    titlePanel("Old Faithful Geyser Data"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            sliderInput("bins",
                        "Number of bins:",
                        min = 1,
                        max = 50,
                        value = 30)
        ),

        # Show a plot of the generated distribution
        mainPanel(
           plotOutput("distPlot")
        ))
)

server <- function(input, output) {

    output$distPlot <- renderPlot({
        # generate bins based on input$bins from ui.R
        x    <- faithful[, 2]
        bins <- seq(min(x), max(x), length.out = input$bins + 1)

        hist(x, breaks = bins, col = 'darkgray', border = 'white', main = Sys.getenv("env_variable_interest"))
    })
}

# Run the application 
shinyApp(ui = ui, server = server)

rkraft
  • 495
  • 4
  • 16
  • doesn't that answer respond ? https://stackoverflow.com/questions/30494050/how-do-i-pass-environment-variables-to-docker-containers?rq=1 – Jrm_FRL Apr 24 '20 at 18:27

2 Answers2

0

I'm not sure, but I would say that you could pass the env vars at the time you build the image, such like this :

docker build --build-arg USER=$USER --build-arg PWD=$PWD -t my_image .

where $USER and $PWD are environment variables already existing in your system (Linux/Windows...). Check for documentation of docker build for more details.

Jrm_FRL
  • 1,394
  • 5
  • 15
  • thanks for reply! I am trying to hand-over the environment variables via run-time (not build time) – rkraft Apr 27 '20 at 06:26
0

Try putting the environmental variable definition in quotes:

docker run -it --rm -p 3838:3838 -e "env_variable_interest=testname" <shiny_image>

Otherwise, I'm guessing it's trying to pass a variable named env_variable_interest=testname, which I assume does not exist on the host operating system.

See docker reference

Marcus
  • 3,478
  • 1
  • 7
  • 16
  • thanks for reply! the syntax I use is correct. the environment variable is also accessible in the container but not in the Shiny server. I try to make the environment variable accessible in the Shiny server – rkraft Apr 27 '20 at 06:27
  • Doing a little digging...when you're accessing it in the container, I'm assuming you are accessing it as `root`. The `shiny` user running the app scrubs environment variables for security reasons (https://community.rstudio.com/t/how-to-access-os-environment-variable-from-shiny-apps-server-r/23822/3) – Marcus Apr 29 '20 at 03:10
  • Can you copy in in a `.Renviron` file into the container (https://community.rstudio.com/t/passing-environment-variable-to-an-app-in-shiny-server/13355) – Marcus Apr 29 '20 at 03:11