R, although powerful, is arguably not as suited for production environments as other programming languages.
When working with Docker and R, especially in long dockerfiles, R poses the problem that it doesn’t issue an error if a package is not successfully installed when building the Docker image. That is, it fails silently, only issuing a warning, and you’ll find out the package wasn’t installed when running your just-built image.
The following excerpt shows the straightforward way to install an R package in a dockerfile. This is not recommended:
FROM r-base:4.2.0
RUN R -e "install.packages('nlme');"
However, we can force a failed installation to issue an error if we place the R session inside a bash session:
FROM r-base:4.2.0
RUN bash -c " \
R -e \"install.packages('nlme');\" \
"
Note the backslashes (\) to escape the double quotes, as well as those to allow a line break.