The free version of the Shiny server uses one R process on a single CPU core. Therefore it can handle few concurrent users. In this repo, we set up a load balancer using Nginx that connects users with many instances of the Shiny server deployed as Docker containers. Developed by Kevin Son, a graduate student in the Ge lab, this method is used to host iDEP and ShinyGO.
- A Linux server with port 80 open. This has been tested on Ubuntu and CentOS.
- Make sure Docker and Docker-compose plug-in are installed and the Docker engine is running.
- Make sure Git is installed.
- Fork this repository, which contains two demo Shiny apps under the shinyapps folder. You can replace these apps with your own once the server is set up.
- Log in to the Linux server, and clone the forked repository. Here I am using the URL for this repo.
cd
git clone https://github.com/gexijin/shinyserver.git
- Build the Nginx Docker image as specified by this Dockerfile.
cd ~/shinyserver/
sudo docker build ./nginx/. -t nginx --pull
- Build the Docker image for the Shiny server, as configured by this Dockerfile. This process might be slow, as all the R packages needed for the Shiny app must be pre-installed, using this R script, which needs to be changed according to your app.
cd ~/shinyserver/
sudo docker build . -t webapp --pull
- Start the containers. Your Shiny app is cloned 5 times in 5 Docker containers, which can run at the same time. You can change the script to run 30 instances. These containers are managed by the Nginx container, as specified in the Docker-compose.yml file.
sudo docker-compose up -d --scale webapp=5
-
The two Shiny apps are hosted at http://xx.xxx.xxx.xxx/app1/ and http://xx.xxx.xxx.xxx/app2/. Note that xxx.xxx is your ip address. App1 reads a local file stored in the data folder using relative path (../../data/). App2 is the demo app from RStudio.
-
Replace the apps and deploy. Now you can clone a local copy of your forked repo to your laptop. Replace the app.R code under the app1 folder with your own R code for the Shiny app. Remember that your data needs to be stored in the shinyserver/data folder, either in the repo or upload directly to the Linux server. After your are done with the development, push your code to GitHub. Then update your code on the Linux server and restart the server:
cd ~/shinyserver/
sudo git pull
sudo sh restart.sh
Please let me know if you have any questions or comments.