How to dockerise a Shiny app

How to port a shiny app into a container

Andrea Spano andreaspano.github.io (Quantide)www.quantide.com
2020-09-20

Prerequisites

In order to build a container with your shiny app inside you need three files:

  1. app.R containing your working shiny app
  2. Dockerfile
  3. Rprofile.site

Starting from your working directory proj, teh file hierarchy must be:


-- proj 
   -- Dockerfile
   -- Rprofile.site
   -- iris
      -- app.R

app.R

Your app.R contains your shiny app. As a first example you may consider the iris example from https://shiny.rstudio.com/gallery/kmeans-example.html. The iris example is also available here. Store app.R inside the application directory say iris.

Dockerfile

Your docker file contains the following instructions:


# where to download the base R docker image from
FROM rocker/r-base

# system libraries commonly required by R packages
RUN apt-get update && apt-get install -y \
    sudo \
    pandoc \
    pandoc-citeproc \
    libcurl4-gnutls-dev \
    libcairo2-dev \
    libxt-dev \
    libssl-dev \
    libssh2-1-dev


# Install required packages
RUN R -e "install.packages(c('shiny', 'rmarkdown'), repos='https://cloud.r-project.org/')"


# copy the app to the image
RUN mkdir /root/iris
COPY iris /root/iris


COPY Rprofile.site /usr/lib/R/etc/

EXPOSE 3333

CMD ["R", "-e", "shiny::runApp('/root/iris')"]

Rprofile.site

The Rprofile.site file states that:


local({
   options(shiny.port = 3333, shiny.host = "0.0.0.0")
})

Build the container

Build docker image with


sudo docker build   --tag iris .

Test by

Assuming that everything worked out correctly, if you issue:


sudo docker run -it --rm -p 3333:3333 iris 

you should see the iris application at 0.0.0.0:3333

Corrections

If you see mistakes or want to suggest changes, please create an issue on the source repository.

Citation

For attribution, please cite this work as

Spano (2020, Sept. 20). andreaspano blog: How to dockerise a Shiny app. Retrieved from https://andreaspano.github.io/posts/2020-09-19-shiny-app-in-a-container/

BibTeX citation

@misc{spano2020how,
  author = {Spano, Andrea},
  title = {andreaspano blog: How to dockerise a Shiny app},
  url = {https://andreaspano.github.io/posts/2020-09-19-shiny-app-in-a-container/},
  year = {2020}
}