How to port a shiny app into a container
In order to build a container with your shiny app inside you need three files:
Starting from your working directory proj
, teh file hierarchy must be:
-- proj
-- Dockerfile
-- Rprofile.site
-- iris
-- 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
.
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')"]
The Rprofile.site file states that:
local({
options(shiny.port = 3333, shiny.host = "0.0.0.0")
})
Build docker image with
sudo docker build --tag iris .
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
If you see mistakes or want to suggest changes, please create an issue on the source repository.
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} }