I used shinyapps.io for my own shiny app. It’s a great service. You can deploy your app for free, test it and show it to other people. But there’s also a downside: The memory an app can use is limited.
So I was looking for another way to deploy my app. So I took a look at Docker.
What is Docker?
A Docker container contains all programs and libraries which are required for running a special application. So it’s easy to transfer or distribute it to another “Docker host” and run the application on it.
Two docker container are separated so they can’t interfere with another. They can only talk with each other using defined ports or directories.
So that’s a way to solve the problem with different package versions or versions of R.
How to put a shiny app into a Docker container?
You have to write a Dockerfile to describe how Docker builds an image which can be started as a Docker container. The Dockerfile is a recipe with several steps. If you change one step all steps before the changed step can be reused. The changed step and all steps after that one must be run again.
I found a nice description of a Dockerfile for a shiny app at this page of statworx.
Building a container with the right R Environment using Renv
The main problem is to install all the R packages needed by your shiny app into your Docker container. Statworx uses the new renv R package management tool from RStudio.
Renv can write all needed packages into a file called renv.lock. If you run
these packages are installed again.
In practice some packages aren’t installed because some requirements on the OS-level aren’t met. You’ll get error messages which guide you to the Linux packages you should install into your container before installing the R packages.
But how does my Dockerfile look like? Here it is:
But let’s break it down into simple pieces:
The base image
That’s an image which contains a base R installation, tidyverse and the shiny server. This image uses Ubuntu as operating system.
Adding Ubuntu packages
So now we install all required (Ubuntu-) packages required for our R-packages:
Installing R packages
Now we install all R packages required by our shiny app. That’s an optimization in respect to the statworx example I did here.
Statworx copies the whole app including the
renv.lock file into the image in this next steps and
installs the R packages.
I copy only the
renv.lock file and install the packages. You’ll see the advantage soon.
That’s the step you’ll get the most errors because of missing dependancies. It’s also the most time consuming step. So you want to run it only if it is really required.
Installing and running the shiny app
Now we copy the shiny app into the image and run it. We also say that the app runs on port 3838 and declare this port to be visible from outside the container.
So now you can see the advantage of splitting the renv.lock file and the whole app: Once you have set up your R environment in your container and you do only changes to your shiny app you can reuse the first and time consuming steps of your container creating process. Docker only has to rerun the last steps: Copying your app and running it.
When running your Docker container on a server you may want to use a reverse proxy such as nginx to map the local port to the standard port 80 resp. 443 and use tls-encryption to protect the traffic.