Dockerizing Shiny Apps
Within the data science world, a commonly cited struggle is getting the results of an analysis embedded in an already existing business process – in other words, having the stakeholder use the data.
Often, it is easier to embed science into a process if the stakeholders have input into the process rather than simply working from flat results. Shiny Apps help Data Scientists to provide flexible sciences that allow for stakeholder input.
What is Docker?
Docker is an open source tool that allows Data Scientists to package applications with all dependencies, packages, and data into a self-sustainable container that can then be deployed to users as needed.
Why Dockerize a Shiny App?
One obvious question exists – if I have access to Shiny Server, why would I Dockerize my Shiny App? Well, you might not! Dockerizing an app is an alternative solution for those who do not have a dedicated Shiny Server, though still wish to deploy apps to stakeholders.
In addition to this, Dockerizing an app allows for the app to be reproducible by anybody. Because the container has all the package, data, and code dependencies necessary to run the app, it should be easily transferrable in the case of work handoff or collaborative coding.
How to Dockerize a Shiny App
The Dockerfile
The key to creating a Docker container is the Dockerfile. The Dockerfile is a set of instructions that tells Docker how to create the container. Below is an example Dockerfile that will Dockerize a Shiny App. The prerequisites for this Dockerfile example are:
The app, and any relevant images, data, etc. are located in a folder named
[object Object]The
[object Object]folder must contain a[object Object]and[object Object]file that run the Shiny app
Example Dockerfile
Now, let’s break down this Dockerfile to look at how it works.
This part tells Docker what source image to use. We are going to be using the shiny source image from rocker. You can check the documentation for this image and see the full code here.
This chunk will install some linux utilities that are necessary to run shiny server. It will also change some of the files from the rocker version of shiny so that we can deploy our app. Specifically, take a look at this line:
Here, we are modifying the default shiny-server.conf file so that we open traffic to any IP (the 0 .0.0.0), and we are keeping the port of our app at the default of
And now we would be deploying the app to port
This line installs whatever R packages we need into the container. Simply add packages within the
If you need an older version of a package, or simply want your container to always use a specific version, you can pass the full URL of the package version you want rather than the name of the package. For example, if we want
This will install the specific version of ggplot2 into our container, and make future-proofing the Shiny App easier.
This tells the Dockerfile to copy our
The last few lines here tell the container that we are declaring a
Deploying the App
Once your Dockerfile is complete, simply create a folder that has the Dockerfile and the
Then, simply navigate to the folder containing the Dockerfile and run the command
For example:
This will begin the creation of the Docker image, using your Dockerfile as instructions.
Once the image is created, you can see deploy the image to the given port by using
For example:
Once this is entered, you are done! The app will now be accessible hosted from your server at the port # specified.
Visit our knowledge hub
See what you can learn from our latest posts.