Deploy your RShiny App Locally with Docker
Dec 10, 2019My favorite way to deploy RShiny locally is to simply package it into a docker image and run it. Running any application in docker makes it easily transportable, and is a generally acceptable way of distributing applications in 2019.
This solution does not require rshiny-server or shinyapps.io, not because I have anything against either of those solutions. I just tend to stick to a few favorite deployment methods to keep my head from spinning straight off my body. ;-)
If you're not familiar with docker then I have a FREE course available. The first module is plenty to get
you up and running and can be completed in an hour or two. For the most part, if you can use a command line you can use docker.
Package your R Shiny App in Docker
Now that we've covered some housekeeping let's get started building your docker image. Like any project, you want to have all your relevant code in a directory. This way it is accessible to the docker build process.
Project Directory Structure
➜ 2019-11-rshiny-app-docker tree
.
├── Dockerfile
├── README.md
├── app.R
└── environment.yml
0 directories, 4 files
This is a very simple example with a single R file that serves our RShiny app, app.R. The Dockerfile has our build instructions, and the environment.yml lists our conda packages, namely r-shiny and r-devtools.
I personally like to install all of my scientific software with conda. If you prefer to install your software another way go for it. What matters is that your dependencies are installed and ready to run your RShiny app.
The RShiny App
This is a very simple example that is taken directly from the RShiny examples github repo, with just a few changes.
Dockerfile
The dockerfile has our docker build instructions. It has a fairly simple syntax. Choose a base image with FROM, run commands during the build with RUN, copy files with COPY and define a startup command with CMD.
Make sure you have made your app.R executable with chmod 777 app.R or this next step won't work.
Conda Env Definition File
Again, installing your software with conda is optional. It is simply my favorite installation method. ;-)
name: r-shiny
channels:
- conda-forge
- defaults
dependencies:
- python=3.6
- r-devtools
- r-shiny
Build the Docker Container
Now that we have our files all ready let's build and run our docker container!
docker build -t r-shiny-app .
Run our RShiny App
Now that we've built our app we can run it!
docker run -it -p 8080:8080 r-shiny-app
You should see a message saying Listening on http://0.0.0.0:8080. Once you see that you're all set! Open up your browser at localhost:8080 to see your application in action!
Wrap Up
That's all it takes to deploy your RShiny application in a Docker container. From there, you can deploy it to a remote server, deploy and scale with Docker Swarm or Kubernetes, or run your application on AWS.
If you'd like to learn more about how I deploy RShiny Apps on AWS I have a Free Guide available when you sign up for my DevOps for Data Scientists Weekly Tutorial List!