Docker RStudio server

Using docker ensures compatibility and reproducibility of R code when versions change. It is particularly useful for geospatial work, as the geospatial packages are notoriously dependent on a changing set of libraries. Having a shiny server running within the same container as the Rstudio server ensures that all shiny apps have accesss to the same packages that they were built with.

Prebuilt container

A prebuilt container can be pulled from dockerhub and should run out the box.

sudo docker run -d -p 8787:8787 -p 3838:3838 --name myrocker  -e PASSWORD=****** -e ROOT=true dgolicher/rocker

It is preferable to link the home directory and the shiny directory to local directories.

## Example
sudo docker run -d -p 8788:8787 -p 3839:3838 --name myrocker -v /home/duncan/shinyapps/:/srv/shiny-server -v /home/duncan/rhome/:/home -v /home/duncan/shinyapps/:/home/rstudio/shiny -e PASSWORD=secret -e ROOT=true dgolicher/rocker

Build file

For future reference, in case I or anyone else, needs to build a version of RStudio Server that is compatible with the one I am currently running. This is quite a large build as it contains grass, saga and postgis for geospatial work. The shiny server is also up and running.

FROM rocker/verse:latest  ## This was 4.0.3 when built

RUN apt-get update \
  && apt-get install -y --no-install-recommends \
    lbzip2 \
    libfftw3-dev \
    libgdal-dev \
    libgeos-dev \
    libgsl0-dev \
    libgl1-mesa-dev \
    libglu1-mesa-dev \
    libhdf4-alt-dev \
    libhdf5-dev \
    libjq-dev \
    libpq-dev \
    libproj-dev \
    libprotobuf-dev \
    libnetcdf-dev \
    libsqlite3-dev \
    libssl-dev \
    libudunits2-dev \
    netcdf-bin \
    postgis \
    protobuf-compiler \
    sqlite3 \
    tk-dev \
    grass  \
    saga  \
    postgresql \
    unixodbc-dev

RUN install2.r --error \
    RColorBrewer \
    RandomFields \
    RNetCDF \
    classInt \
    deldir \
    gstat \
    hdf5r \
    lidR \
    mapdata \
    maptools \
    mapview \
    ncdf4 \
    proj4 \
    raster \
    rgdal \
    rgeos \
    rlas \
    sf \
    sp \
    spacetime \
    spatstat \
    spdep \
    geoR \
    geosphere \
    devtools \
    dismo \
    ade4  \
    tmap \
    ctv  \
    sjPlot 


RUN /rocker_scripts/install_shiny_server.sh

Copy this text into a file and save it in a directory with the name “Dockerfile”. Then build with a new name.

sudo docker build -t myrocker .

sudo docker run -d -p 8787:8787 -p 3838:3838 --name myrocker  -e PASSWORD=****** -e ROOT=true myrocker

Adjustments after building

There were a few problems with the version produced by the build that were fixed within the container. In particular the mapview version did not produce knitted output. These fixed have been pushed into the built container.

# Fixing mapview
remotes::install_github("r-spatial/mapview")
## Install jags from cran
sudo apt install r-cran-rjags 
## In case posgresql not installed properly
sudo apt-get install postgresql-server-dev-12
sudo service postgresql start
sudo passwd postgres
sudo -u postgres psql -c "alter user postgres with password 'postgres';"
createdb geostats
conn <- dbConnect("PostgreSQL", host = "localhost",dbname = "geostats",user = "postgres", password = "postgres")
### With an external postgis and geoserver etc etc ....
sudo docker run -d -p 8788:8787 -p 8790:3838 --name burocker --link postgis-plr:postgis -v /home/duncan/shinyapps/:/srv/shiny-server -v /home/duncan/rstudio/:/home/rstudio/rstudio -v /home/duncan/webpages/:/home/rstudio/webpages -v /home/duncan/shinyapps/:/home/rstudio/shiny  -v /home/duncan/rhome/:/home  -v  /home/duncan/geoserver_data2:/home/rstudio/geoserver -e ADD=shiny -e PASSWORD=egacricketcaptainname -e ROOT=true dgolicher/rocker