5 MIN READ
Blog
8451 380 X 240 Shutterstock 1517113610 M
5 MIN READ
Blog

Dockerizing Shiny Apps

By: David Schappler, Data Scientist
8451 380 X 240 Shutterstock 1517113610 M

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:

  1. The app, and any relevant images, data, etc. are located in a folder named

    [object Object]

  2. The

    [object Object]
    folder must contain a
    [object Object]
    and
    [object Object]
    file that run the Shiny app

Example Dockerfile

[object Object]

Now, let’s break down this Dockerfile to look at how it works.

[object Object]

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.

[object Object]

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:

[object Object]

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

[object Object]
. If we wanted to change port numbers, we would change only the second
[object Object]
to whatever port number we wanted. For example, to change the port number to 1738, we would change the line to look like this:

[object Object]

And now we would be deploying the app to port

[object Object]
instead of
[object Object]
.

[object Object]

This line installs whatever R packages we need into the container. Simply add packages within the

[object Object]
argument that you want installed. This runs the command using R, so you can provide any additional function arguments that
[object Object]
accepts. For full documentation, see here.

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

[object Object]
to be frozen at version
[object Object]
, we could add this line to our Dockerfile:

[object Object]

This will install the specific version of ggplot2 into our container, and make future-proofing the Shiny App easier.

[object Object]

This tells the Dockerfile to copy our

[object Object]
folder into the
[object Object]
folder in our container. The end pathway will be
[object Object]
. We then chmod our folder so that the files can be run by the container.

[object Object]

The last few lines here tell the container that we are declaring a

[object Object]
for the container, and that user is shiny. We then expose the app to our port number so that we can access the Shiny App via a URL. Lastly, we run the shiny-server shell script (provided by the
[object Object]
image) to kick off shiny server, running our app in the __
/srv/shiny-server/app__ folder.

Deploying the App

Once your Dockerfile is complete, simply create a folder that has the Dockerfile and the

[object Object]
folder in it.

Then, simply navigate to the folder containing the Dockerfile and run the command

[object Object]

[object Object]
, in this case, is the name you want to give your Docker image.
[object Object]
is the file path to the Dockerfile (if you are in the folder already, just enter .)

For example:

[object Object]
.

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

[object Object]

[object Object]
must match the name of the Docker image you created before, and
[object Object]
should match the port # in your Dockerfile.

For example:

[object Object]

Once this is entered, you are done! The app will now be accessible hosted from your server at the port # specified.

David Schappler, Data Scientist

We’re leading a data revolution in the retail business, and we’re looking for partners who are ready for a deeper, more personal approach to customer engagement.

Let’s connect