Intial commit to create and use R docker container

This commit is contained in:
Alex Gebben Work 2026-01-08 15:40:30 -07:00
parent 8f0555d861
commit b378d4cc80
7 changed files with 97 additions and 1 deletions

32
Dockerfile.Full Normal file
View File

@ -0,0 +1,32 @@
#Date Created: Jan 8, 2026
#Author Alex Gebben <agebben@mines.edu,agebben@uwyo.edu>
FROM rocker/r-ver:4.5.2
LABEL org.opencontainers.image.authors=agebben@uwyo.edu
RUN apt-get update \
&& apt-get install -y sudo
#Create a sudo user not called root. This avoids permision issues when working with the files created in the docker and saved on a local unix system
#RUN useradd -m -s /bin/bash rdocker
RUN adduser --disabled-password --gecos '' rdocker \
&& adduser rdocker sudo \
&& echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers \
&& chmod 777 /usr/local/lib/R/site-library
USER rdocker
#Install all necessary packages for anaylysis, file manipulation, or for the required R packages to function.
RUN sudo apt-get install -y libmariadb-dev imagemagick ghostscript libpoppler-cpp-dev vim wget curl git libcurl4-openssl-dev build-essential texlive-full
#Install the R packages used in the R data creation scripts run on on the docker container.
RUN install2.r tidyverse fixest twang pdftools gt scales vars curl forecast uuid corrplot foreach cluster factoextra xtable Formula miscTools MASS kableExtra clisymbols fredr readxl lubridate tidycensus zipcodeR stargazer sandwich lmtest ggnewscale viridis ggthemes MatchIt optmatch aTSA tsDyn bruceR svars tidyquant zoo eia renv BiocManager miscTools logr && rm -rf /tmp/downloaded_packages
#Packages used in DCchoice nuclear contigent valuation paper which require a special install method
#Icens is required to be installed using BiocManager for DCchoice to be installed.
#"interval" required Icens to be installed
#BiocManager Version will need to be updated periodically.
RUN R -e 'BiocManager::install(version = "3.22")'\
&& R -e 'BiocManager::install("Icens")' \
&& R -e "install.packages('DCchoice',dependencies=TRUE)" \
&& install2.r interval \
&& rm -rf /tmp/downloaded_packages
WORKDIR /R-Code
RUN sudo chmod -R 777 /R-Code
#Add VIM R IDE to work in the Container, not required.
#Packages used for a vim-R IDE. Can be removed if not used
#Add vimrc which can be used with vimplug to run an R ide to perform R edits in the terminal.
ADD VIM_R_IDE/.vimrc /home/rdocker/.vimrc

21
Dockerfile.Minimal Normal file
View File

@ -0,0 +1,21 @@
#Date Created: Jan 8, 2026
#Author Alex Gebben <agebben@mines.edu,agebben@uwyo.edu>
FROM rocker/r-ver:4.5.2
LABEL org.opencontainers.image.authors=agebben@uwyo.edu
#Create a sudo user not called root. This avoids permision issues when working with the files created in the docker and saved on a local unix system
#RUN useradd -m -s /bin/bash rdocker
RUN apt-get update \
&& apt-get install -y sudo\
&& adduser --disabled-password --gecos '' rdocker \
&& adduser rdocker sudo \
&& echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers \
&& chmod 777 /usr/local/lib/R/site-library
USER rdocker
#Install all necessary packages for anaylysis, file manipulation, or for the required R packages to function.
RUN sudo apt-get install -y vim curl git && install2.r dplyr renv && rm -rf /tmp/downloaded_packages
WORKDIR /R-Code
RUN sudo chmod -R 777 /R-Code
#Add VIM R IDE to work in the Container, not required.
#Packages used for a vim-R IDE. Can be removed if not used
#Add vimrc which can be used with vimplug to run an R ide to perform R edits in the terminal.
ADD VIM_R_IDE/.vimrc /home/rdocker/.vimrc

13
Make_Docker.sh Normal file
View File

@ -0,0 +1,13 @@
#
ALL_PACKAGES=true
MINIMAL_PACKAGES=false
#Load all R packages in the docker
if [ "$ALL_PACKAGES" = true ]; then
docker build --file Dockerfile.Full -t acg/r_full .
docker build --file Dockerfile.Full -t acg/r_full:1.0 .
fi
##Minimal
if [ "$MINIMAL_PACKAGES" = true ]; then
docker build --file Dockerfile.Minimal -t acg/r_minimal .
docker build --file Dockerfile.Minimal -t acg/r_minimal:1.0 .
fi

View File

@ -1,3 +1,6 @@
# R-Docker-Starting-point
A starting point to run R in docker with renv for publication reproducibility. An IDE is installed to allow for R editing on any computer. The container comes from the docker hub Rocker r-sources. Customization is made to run a vim IDE of R, and to load libraries used in my workflow.
1) If the r docker images have not been created run the "Make_Docker.sh" file.
2) run "source Alias_Docker.sh" to add terminal shortcuts for running the docker. These are r-full or r-minimal for entering the docker container to work in r, and r-full-script or r-minimal-script for running a local script in the container without any interaction. These commands can be copied to bash_alias
A starting point to run R in docker with renv for publication reproducibility. An IDE is installed to allow for R editing on any computer.

14
VIM_R_IDE/.vimrc Normal file
View File

@ -0,0 +1,14 @@
if empty(glob('~/.vim/autoload/plug.vim'))
silent !curl -fLo ~/.vim/autoload/plug.vim --create-dirs
\ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
autocmd VimEnter * PlugInstall --sync | source $MYVIMRC
endif
" Specify a directory for plugins
" - For Neovim: stdpath('data') . '/plugged'
" - Avoid using standard Vim directory names like 'plugin'
call plug#begin('~/.vim/plugged')
Plug 'jalvesaq/Nvim-R'
Plug 'frazrepo/vim-rainbow'
call plug#end()
let g:rainbow_active = 1
" Initialize plugin system

4
VIM_R_IDE/README_VIM.md Normal file
View File

@ -0,0 +1,4 @@
This file contains a ".vimrc" file that can be loaded to the /root/ directory. Then when using vim it will download and install vimplug, as well as the nvimr plugin. This allows vim to act as an IDE, so the code can be configures in the docker image, and not simply run with the script.
If this plugin fails to load it can be commented out in the Dockerfile (make script at the end). This is a personal preference about how to edit the code.

9
r-Docker-Alias.sh Normal file
View File

@ -0,0 +1,9 @@
#Create a terminal shortcut to open the current directory in the docker
#Docker images with all the R packages I have ever used
alias r-full='docker run -ti --rm -v ./:/R-Code/ acg/r_full:latest bash' #Pull in the current directory into the docker image. Enter the image to modify with a r vim IDE preloaded.
alias r-full-script='docker run -v ./:/R-Code/ acg/r_full:latest Rscript' #Run a r-file as a script rather than Interactively
#Docker images with minimal packages loaded to save space
alias r-min='docker run -ti --rm -v ./:/R-Code/ acg/r_minimal:latest bash'
alias r-min-script='docker run -v ./:/R-Code/ acg/r_minimal:latest Rscript'