Using Shiny Server in Docker

A quick note on how to use the Shiny Server Docker image, rocker/shiny.

I’m a big believer in starting with the simplest possible setup, getting that to work and then adding complexity in layers. We’ll start with a simple Shiny application in app.R.

library(shiny)

ui <- fluidPage(
  titlePanel("Example Shiny Application")
)

server <- function(input, output) {
}

shinyApp(ui = ui, server = server)

It really doesn’t get much simpler than that.

We want to wrap that up in a Docker image.

FROM rocker/shiny:4.1.0

RUN rm -rf /srv/shiny-server/*

WORKDIR /srv/shiny-server/

COPY ./app.R ./app.R

The Dockerfile doesn’t do anything fancy:

  1. import a specific version of the rocker/shiny image
  2. delete all of the example files
  3. change the working directory to the location where the server will look for apps and
  4. copy the app onto the image.

You don’t explicitly run the application, just put it into the server folder and the server does the rest. The application will reside at /srv/shiny-server/app.R on the image.

With those two files in place we can build the image, giving it a tag of shiny-in-docker.

docker build -t shiny-in-docker .

That should only take a short while. Then we can run it, exposing the Shiny Server on port 3838.

docker run --rm -p 3838:3838 shiny-in-docker

Once the container has launched you should be able to view the application in a browser at http://127.0.0.1:3838/.