Shiny application in production with ShinyProxy, Docker and Debian
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
You created some great Shiny applications, following our advice of Shiny packaging for example, and you want to put them into production, self-hosting, so that others can enjoy them, without limitations, on the Internet or on an internal server of your company? ShinyProxy is for you!
ShinyProxy v2.0 has recently been released. What a great opportunity to talk about its implementation!
A major interest of ShinyProxy is its ability to create an independent Docker container for each user who connects to your Shiny application. This overrides the limitations on the number of users faced with ShinyServer. Are you interested? Follow the installation procedure….
ShinyProxy installation
For our installation, we chose to use a Debian Jessie server. We let you make the necessary code adjustments for other Linux distributions. (For example, for Ubuntu, replace ‘debian’ with ‘ubuntu’ in the appropriate codes). In addition, there are two ways to deploy ShinyProxy, either using JAVA or using a Docker container. You will be able to choose either one or the other after this article but in both cases you will have to install Docker on your server!
ShinyProxy and Java
First step: install Java
ShinyProxy is a software that works with Java. You need to check if Java is installed and if so, which version. In your terminal, enter the command: `sh Java -version
If you receive a message like this:
openjdk version "1.8.0_xxx"
Then Java 8 is already installed on your server. Otherwise, you can install either (1) the free version:
sudo apt install default-jre default-jdk
or (2) the Java8 version of Oracle with webupd8team’s PPA:
echo "deb http://ppa.launchpad.net/webupd8team/java/ubuntu xenial main" | \ sudo tee /etc/apt/sources.list.d/webupd8team-java.list echo "deb-src http://ppa.launchpad.net/webupd8team/java/ubuntu xenial main" | \ sudo tee -a /etc/apt/sources.list.d/webupd8team-java.list sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys EEA14886 sudo apt-get install oracle-java8-installer
We check if the installation worked well:
java -version openjdk version "1.8.0_xxx"
Perfect, then we can continue our installation.
Second step: Install Docker
As explained above, ShinyProxy allows you to launch a Docker container from our Shiny application. So we will install Docker CE on our server.
There is a need for some dependencies. It is also better to install the latest version of Docker CE using Docker servers as a third-party repository.
In your terminal:
sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ gnupg2 \ software-properties-common curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add - sudo add-apt-repository \ "deb [arch=amd64] https://download.docker.com/linux/debian \ $(lsb_release -cs) \ stable"
sudo apt-get install docker docker-ce
To find out if everything went well, you can run a little test to ask Docker to say hello:
docker run hello-world
If you get a message like this:
Then Docker is installed.
One last small modification….
For ShinyProxy to work, you need to modify the Docker file /lib/systemd/systemd/system/docker.service
and restart Docker (use your favorite text editor):
sudo nano /lib/systemd/system/docker.service
Modify the line with ‘ExecStart’.
ExecStart=/usr/bin/dockerd -H fd:// -D -H tcp://127.0.0.1:2375
Then restart Docker
sudo systemctl daemon-reload sudo systemctl restart docker
Third step: Install ShinyProxy
It’s time to go back to what really interests us, the installation of ShinyProxy to deploy our Shiny applications!
First, you need to clone the GitHub repository of the ShinyProxy software (you can also download the .zip from github):
git clone https://github.com/openanalytics/ShinyProxy.git
We should find a “ShinyProxy” folder. We get into the folder and install ShinyProxy. You may first need to install “maven”:
cd ShinyProxy/ sudo apt install maven mvn -
Then, we can test if the examples can be run. To do this, you need the ShinyProxy template and the Docker images of the Shiny applications proposed in the demo.
mkdir app-shiny cd app-shiny/ git clone https://github.com/openanalytics/ShinyProxy-template.git cd ShinyProxy-template/ docker build -t openanalytics/ShinyProxy-template .
Then, you launch ShinyProxy: You have to go up in the tree structure to find the folder “ShinyProxy/target”.
Change this command with the version of ShinyProxy you have.
cd ShinyProxy/target/ java -jar ShinyProxy-2.0.2.jar
Go to https://localhost:8080
. You should see this page:
To be able to customize your ShinyProxy (deploy your applications, change the ShinyProxy home page), you have to modify the application.yml
file and the different .html
contained in the different sub-folders of shinproxy/
. For example, we can modify the configuration file application.yml
(this file allows us to define the applications to be deployed and other ShinyProxy configurations):
cd ../src/main/ressources/ nano application.yml
Let’s remove a demo application for example. For that you just have to delete the following lines:
- id: 06_tabsets container-cmd: ["R", "-e", "ShinyProxy::run_06_tabsets()"] container-image: openanalytics/ShinyProxy-demo container-network: sp-example-net cd ../../../ mvn -U clean install
Then, restart with the .jar
:
cd target/ Java -jar ShinyProxy-2.0.2.jar
But all these steps take a long time. Following each change, it is necessary to recompile, restart,… The round trips are therefore frequent and a waste of our time. Besides, here it doesn’t run in the background, the terminal needs to be up (although you can use nohup
). This leads us to use the “ShinyProxy in container” version.
ShinyProxy in Docker container
Do not forget to install docker too to follow this part (cf. Second step: Install Docker)!
To make our lives easier, there are ShinyProxy templates that can be easily deployed with Docker. You will find an example here!
First, it is necessary to clone the GitHub repository:
mkdir ShinyProxy-docker cd ShinyProxy-docker/ git clone https://github.com/openanalytics/ShinyProxy-config-examples.git
We get four files. For this example, we will use the folder starting with 02
. It contains two important files: (1) the application.yml
file that allows the configuration of our ShinyProxy and (2) the dockerfile
, which allows the ShinyProxy image to be mounted.
Application.yml
We will detail the important code lines of this file. We will also be able to discuss the differences between versions 1.1 and 2.0 of ShinyProxy because the writing of the `.yml’ has changed.
Changes between versions are in the part dedicated to Shiny applications. docker
is replaced by container
, name
by id
and groups
by access-groups
.
In ShinyProxy version 1.1, we had:
apps: - name: 01_hello display-name: Hello Application description: Application which demonstrates the basics of a Shiny app docker-cmd: ["R", "-e", "ShinyProxy::run_01_hello()"] docker-image: openanalytics/ShinyProxy-demo groups: [scientists, mathematicians]
And in version 2.0, we have:
specs: - id: 01_hello display-name: Hello Application description: Application which demonstrates the basics of a Shiny app container-cmd: ["R", "-e", "ShinyProxy::run_01_hello()"] container-image: openanalytics/ShinyProxy-demo container-network: sp-example-net
Here is the example of the .yml
proposed in the 02
folder that we downloaded earlier:
proxy: port: 8080 authentication: simple admin-groups: admins users: - name: jack password: password groups: admins - name: jeff password: password docker: internal-networking: true specs: - id: 01_hello display-name: Hello Application description: Application which demonstrates the basics of a Shiny app container-cmd: ["R", "-e", "ShinyProxy::run_01_hello()"] container-image: openanalytics/ShinyProxy-demo container-network: sp-example-net - id: 06_tabsets container-cmd: ["R", "-e", "ShinyProxy::run_06_tabsets()"] container-image: openanalytics/ShinyProxy-demo container-network: sp-example-net logging: file: ShinyProxy.log
In this file, port
indicates that the container will launch on port 8080
, authentication
gives us the type of identification chosen, in our case it is “simple”. In other words, we create users
which can be long (there are many others). Then we find the specs
part for the different Shiny applications.
A brief focus on interesting options:
heartbeat-timeout: 60000
allows you to configure how long (in milliseconds) the container remains active if it is not used. After that, he is stopped.hide-navbar : true
hides the navigation bar.
There are many more options that you can find here.
The Dockerfile
The Dockerfile allows us to build our Docker image according to what we are interested in. In the 02
folder, it allows you to directly change the .yml
but you might also want to change the home page for example (04
folder for the most curious).
The installation of ShinyProxy
It is now time to practice.
First, we need to create a network so that ShinyProxy can communicate with the containers of the Shiny apps. Let’s name this network sp-example-net
:
sudo docker network create sp-example-net
Then, we have to create our ShinyProxy image: You need to make sure you’re inside the right folder (in 02-containerized-docker-engine)!
cd 02-containerized-docker-engine docker build -t mon_ShinyProxy .
Finally, let’s run our container:
sudo docker run -d -v /var/run/docker.sock:/var/run/docker.sock --net sp-example-net -p 8080:8080 mon_ShinyProxy
We save a lot of time compared to the off-Docker version. In two command lines, you can restart your ShinyProxy if you made changes to the .yml
.
And there are many other possibilities with ShinyProxy and Docker.
That’s it ! You have all the information you need to put your Shiny application into production! Don’t forget to turn your application into a package! If this is not the case, you can read our article on designing a shiny template in an R package. And if you don’t make it, you can always ask us !
The post Shiny application in production with ShinyProxy, Docker and Debian appeared first on (en) The R Task Force.
R-bloggers.com 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.