Using Environment Variables
This section focuses on setting environment variables during the image’s run time.
Environment variable are a great tool to make your code dynamic and flexiable. Setting environment variable during the run time enables to pass useful information to the container, such as passwords and credentials or any other information. For example, let’s add to the r-base
image an environment variable named MY_VAR
with the value of MY_VALUE
:
docker run --interactive --tty \
-e MY_VAR="MY_VALUE" \
-v ~/Personal/tutorials/r-medicine-vscode-workshop/scripts/:/scripts \
-v ~/Personal/tutorials/r-medicine-vscode-workshop/data/:/data \
rocker/r-base
As before, this will open an R session, and we can use the Sys.getenv
function to print the variable value:
WARNING: The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested
R version 4.4.0 (2024-04-24) -- "Puppy Cup"
Copyright (C) 2024 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> Sys.getenv("MY_VAR")
[1] "MY_VALUE"