Deploy R Shiny Apps with Docker – Windows 10 Home Older Build

Deploy R Shiny Apps with Docker – Windows 10 Home Older Build

There are a number of existing tutorials that outline how to deploy an R Shiny App with Docker. R is great programming language for performing statistical analysis – the community is rich with packages that are finding new uses every day. Shiny is a web frame work for R – you can create a UI for end users to interact with your code in a web browser.

Docker is a platform that can be used to deliver software packages (and dependencies) consistently across different machines. The benefit is that you can have another computer with docker run/reproduce your code without it breaking. Our use case here is having our R code that runs locally also run on a cloud web-server. I highly recommend checking out the documentation – for understanding the essential details.

This guide will highlight the pitfalls and workarounds I ran into when setting up a shiny app via docker.

My PC was running an older build of Windows 10 Home with a Ryzen CPU – this guide will likely be helpful if you have similar specs.

I want to credit the great tutorials from various bloggers that have written on this topic – I’m following their work closely but there are a few edits in the process I need in order to get my build to work:

Install Docker Toolbox

No alt text provided for this image

If you’re running an older build of Windows 10 Home Docker Desktop won’t work for you. You might get the error message displayed above. You’ll need to install Docker Toolbox as a work around. In simple terms Docker Toolbox allows you to set up a Linux virtual machine to run docker – you can interact with the virtual machine through the Docker Quickstart Terminal application and the windows command line.

Disable VTX Check

If you have an AMD Ryzen CPU, you might run into an error when trying to launch the Docker Quickstart Terminal application:

It’s very likely that you already have virtualization enabled in BIOS, but check anyway. If it is in fact enabled and you still get this message open Windows Power Shell as Admin and run:

To ensure docker is correctly set up fire up the Docker Quickstart Terminal. If all goes well you should get an image of a friendly whale.

Run Docker’s Hello World + Get Familiar with Docker + Linux Commands

Get docker hello world with:

You should get a hello message from docker printed to the terminal. Now you’re at a point where you can test out some useful docker commands. I’m listing a few here that I’ve found useful:

Show Running Containers

Show Containers (Including Previously Running Containers)

Run a Docker Image

Stop Running a Docker Image

Execute a command in a running docker container

Examples of executing a command in a running Docker Container:

list contents of directory at a path

view file contents of file in directory

Copy files to docker container

Get build details of running docker container

Remove docker image

Remove docker container

Now its Time – Prepare Your Shiny App

On your windows machine you’ll want to create a directory within a folder to house your shiny app. Your_folder should have the following structure:

If you’ve created a shiny app before the www folder, server.r and ui.r should be familiar to you.

  • The www folder is special folder for css and images that you may want to use in your shiny app.
  • server.r defines server logic in R
  • ui.r handles ui elements in R

Dockerfile, shiny-server.conf & shiny-server.sh may be new if this is your first time Dockerizing an R Shiny app.

  • Dockerfile – this defines a docker image – your telling docker what dependencies to include in your docker image
  • shiny-server.conf – this defines your server configuration
  • shiny-server.sh – define your log directories and execute shiny-server

Dockerfile

I’ve found it easiest to start by using rocker-org’s shiny + tidyverse packages image – this is what’s happening in the first two lines. Next you want to install system libraries that are of general use and any system libraries that your R packages may require as dependencies. If you get an error referencing an R package and a non-zero error code the R package most likely failed to install because you’re missing one of it’s system dependencies. Check all your R imports and make sure all system library dependencies are installed.

Afterwards install the R packages that you need – a few listed here for example purposes. Notice i put the R sf package install separately so that docker could cache previous steps if an error occurred.

When I do end up building a docker image – it will be from with in the you_folder path on the local PC (i.e. – make sure you’re in that directory when building the image). That way we’re able to copy the app contents, shiny-server.conf and shiny-server.sh to the appropriate directories in the image.

Observe I need to install and use dos2unix to get the shiny-server.sh file to run on the virtual machine. I also copy app contents, expose the port I need and run the unix ready shiny-server.sh file.

shiny-server.conf

This file defines the server configuration. The key things to change here are the port number and site directory. In the example below the port number is 80 and root directory is /srv/shiny-server/app – so when we eventually type in the URL we will not need to specify any ‘/’ or sub directories. If we specified a level up, for example /srv/shiny-server our url would need to be appended with /app.

shiny-server.sh

In this file we tell shiny server where and how to log and tell shiny server to start.

Build your docker image

From Docker Quickstart Terminal cd into your_folder. Then build a docker image with:

Get Your Virtual Machine’s IP Address

You should get an ip address along the lines of xxx.xxx.xx.xxx – this is what you need to type into the browser once you’re ready to run your app.

Run Your Docker Image

After you build your image and have your IP, run your image with (80 is the port we specified earlier in the Dockerfile and shiny-server.conf files and make sure it matches whatever port you specify)

Now head over to the ip you got earlier (include :port# if it’s anything other than 80). Your shiny app should run – test it out and ensure it works.

Turn your Dockerfile into a tarball file

Now you can upload this tarball to the cloud host of your choice. If the host has a docker image available you can run your image in detached mode – your shiny app will be available in your browser at your cloud server’s IP.

Leave a Reply

Your email address will not be published. Required fields are marked *