Deploying a Shiny Flexdashboard with Docker

[This article was first published on Stories by Tim M. Schendzielorz on Medium, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Robust deployment of beautiful dashboards.

Example Flexdashboard. From

This is part two of a growing series on data reporting with micro services. For building this dashboard see part one, Shiny Dashboards with Flexdashboard. For deploying dashboards at scale in production with a Shiny Proxy server, see part three.

Dashboards are a fantastic way to deliver data insights fast, intelligible and consistently. Besides designing aesthetic and useful dashboards, a major challenge is deployment. In most businesses, several people besides Data Analysts and various technologies are involved. This leads to a slow deployment process and time consuming dependency management. In this article, we will learn how to containerize a previously build R Flexdashboard with Shiny reactivity with Docker to make a web app that is more easily to reproduce, deploy, scale and manage.

Why Docker?


Docker is the one of the fastest growing open source technologies as of today. Docker containers are sandbox environments for running applications. Instead of emulating a whole machine, only a Linux OS plus libraries is emulated. Such container is more lightweight than a Virtual Machine and running applications containerized has a lot of advantages. Only resources assigned to the container are used and the environment for your application is always the same, regardless on which host it is deployed. That makes Docker the favorite tool for many companies to deploy reproducible and secure applications. An application is more efficient to manage and debug if it runs isolated in a container with defined connections to other containers and the host. Additionally, software is faster to set up due to reduced dependency issues and easy to scale. Furthermore standardized build, test and deployment environments enable fast and robust CI/CD cycles for software development. Various services and applications can be managed and load balanced via container orchestration tools, for example Docker Swarm or Kubernetes.

Building the Docker container

Get started

Containers are build from images which are defined by instructions in a Dockerfile. For each instruction an intermediate image is build and used as base image to build upon. There are already a lot of ready to use images available for download on Dockerhub. You can use these images as base for your own image, too. After you installed Docker for your host system, you can download public images via docker pull in your terminal. We will build the container for the Shiny Flexdashboard upon Open Analytics R Shiny image. This image contains basic R, necessary linux libraries and the Shiny and Rmarkdown R packages. You can see their corresponding Dockerfile on GitHub. You can download the image via docker pull openanalytics/r-shiny.

The Dockerfile

You can check out this Dockerfile and the file for the Shiny Flexdashboard in my GitHub repo.

The Dockerfile specifies how the image is build and in what sequence. FROM openanalytics/r-shiny specifies the base container image, the instructions after are explained with comments. You can get an overview of Dockerfile instructions in the Docker documentation or on this cheat sheet. If you do not have an Flexdashboard Rmd file and want to deploy just a Shiny app, you have to alter the CMD instruction at the end either like this or as described in this medium article.

Build the image

An image is build from the Dockerfile with command docker build. If you have cloned my GitHub repo, run following command in the directory of the Dockerfile docker build -t example_dashboard ., this will build the image from the Dockerfile and name it example_dashboard. The build process will run quite a few minutes the first time as you download and install the dependencies. The nice thing is, once you completed a step it is cached and you do not have to run it completely when you change other steps in your Dockerfile or you can even use the same cached step for another Docker image. You can see all available images on your host with docker image ls.

Terminal output of the Docker image building

Run the container

To make a container from an image use command docker create and use command docker start with the name of the image to start a container. Conveniently, there is a command which combines both into one that we will use here, docker run. This command will even try to pull images from Docker Hub if it can not find the image name on your host.

We spin up the container with docker run –name=example_dashboard_container –rm -p 3838:3838 example_dashboard. This maps the (previously exposed) port 3838 from the Docker container to the same port 3838 of the host machine. Parameter –rm indicates that we want the container to be removed if it was stopped and we specify the container name (which is not necessary) with –name=. If you want to be able to use your terminal further, set parameter -d for running in detached mode. You can still view the container output via docker logs example_dashboard_container.

You can then access the Shiny Flexdashboard inside the Docker container rendered by your browser by navigating to http://localhost:3838. Et voilà, you see the locally deployed Flexdashboard with Shiny elements which will always give the same results, no matter on which machine it is deployed and without having to manage dependencies and versions.

The deployed example dashboard. Try it out out on

Deploy the container

For deploying the container with the Flexdashboard, you could build the image again from the Dockerfile on your own server with a Docker installation. Way more convenient is it to save your image via docker save -o ~/example_dashboard.tar example_dashboard and copy the file to your server. Then load the image via docker load -i example_dashboard.tar and run the container from it. You can reach the dashboard with the IP address of your server instead of localhost in your browser. If you have a domain, you can also add it so your dashboard can be reached through it.

You can set up a reverse proxy like nginx that routes your traffic to various containers depending on the subdomain. Yes, there is a Docker image for that, too.

How to deploy Docker containers to a cloud provider is well documented and you will find instructions and community examples on the web, e.g. for AWS, Google Cloud and DigitalOcean. If you want to deploy a Shiny app/Flexdashboard at scale in a professional setting, a valuable open source tool is the Shinyproxy server from OpenAnalytics. For deploying apps with Shinyproxy, please see part three of this series.

This article was also published on

Deploying a Shiny Flexdashboard with Docker was originally published in Analytics Vidhya on Medium, where people are continuing the conversation by highlighting and responding to this story.

To leave a comment for the author, please follow the link and comment on their blog: Stories by Tim M. Schendzielorz on Medium. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)