This page looks best with JavaScript enabled

#4 Automated installation of R web server for Shiny and RStudio with Docker Compose

In this article I talk about how to use “Docker Compose” to automate the deployment of an Apache Web server that gives access (via proxy) to Shiny applications and RStudio. A single command allows you to prepare the entire environment from scratch: the servers (Apache, Shiny and Rstudio) are downloaded, installed in isolation, and a private network is set up to let these applications communicate. Only dependency is to have Docker Compose installed on your computer.

This article is the continuation of the previous article where I explained the reasons and advantages of this configuration and how to do the manual installation and configuration. In this article I explain how to obtain a similar result with Docker technology, in a fully automated way with “Docker Compose”.

Automation

This is the sole command. Do you think you can do it? :-)

docker-compose up

Docker and Docker Compose

What are they for?

Let’s start with the definition:

Docker is an open-source project that automates the deployment (delivery or release to the customer, with relative installation and commissioning or operation, of an application or software system typically within a corporate IT system) of applications inside software containers, providing an additional abstraction thanks to virtualization at the Linux operating system level. Wikipedia IT

Very briefly, let’s say it looks like a virtual machine dedicated to a single application. Imagine a docker container as composed of an executable, a portion of the file system containing the ecosystem needed by the application, a dedicated network interface. The container interacts with the environment via network connection or shared file system folders. The advantage of this configuration is that the applications in the containers do not depend on the software installed on the server, but they carry the dependencies. In this way, incompatibility problems are avoided: if the container works on one computer it will also work on the others. This allows you to use software without worrying about having the exact same environment installed, to make updates “by application”, and to rollback very easily.

Compose is an application that allows you to assemble a number of Docker containers and use them as a single service. A single configuration file describes how to configure them, connect them to each other via a virtual and dedicated network.

How are they used?

For convenience I have prepared a Git Repository with the configuration: https://github.com/rinproduction/docker-rweb/

The YAML file docker-compose.yml contains the composition configuration:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
version: '3.5'

services:
  apache:
    image: 'bitnami/apache:latest'
    user: "0:0"
    ports:
      - '80:80'
      - '443:443'
    networks:
      - sp-net
    volumes:
      - ./config/apache/httpd.conf:/opt/bitnami/apache/conf/httpd.conf:ro
      - ./config/apache/r-ssl.conf:/vhosts/my_vhost.conf:ro
      - ./config/ssl:/etc/ssl:ro
    environment:
      - APACHE_HTTP_PORT_NUMBER=80
      - APACHE_HTTPS_PORT_NUMBER=443

  shiny:
    image: 'rocker/shiny'
    networks:
      - sp-net

  rstudio:
    image: 'rocker/rstudio'
    networks:
      - sp-net
    environment:
      - PASSWORD=rinproduction
      - DISABLE_AUTH=false
      # - ADD=shiny

  
networks:
  sp-net:
    name: rweb_sp-net    

The configuration file contains three services (three docker containers) and one network (sp-net).

Apache is the web server used. The apache docker image is denoted by bitnami/apache:latest, which means that it is located at https://hub.docker.com/r/bitnami/apache and takes the latest version. At this address you will find the documentation of the Docker image. The ports parameter indicates which ports to share with the world outside the Compose: in this case the ports are related to HTTP and HTTPS services. The http://localhost and https://localhost addresses work precisely because ports 80 and 443 are shared between Docker and the host (the computer). volumes indicates how to share configuration files between the host and one of the containers. environment is used to configure some environment variables used in the container.

The images containing the Shiny and RStudio applications are taken from https://hub.docker.com/r/rocker/. Here you will find a good number of updated and ready-to-use Docker images.

Note that in the docker-compose.yml all containers are connected to the same subnet: sp-net. This allows Apache when it receives a connection to forward it to Shiny or RStudio.

The next section contains a little guide on how to use this configuration in practice.

How-to

Requirements

A computer with a Linux distribution on which Docker and Docker Compose is installed or can be installed. Note that compared to the article with the manual procedure, which asked that RStudio and Shiny Server be present and installed, here it is not required because the procedure also installs these piece of software.

Install Docker and Docker Compose

To install Docker use the specific instructions for the distribution you are using. I write here the instructions that I have tested on Ubuntu 20.04.

sudo apt install docker.io docker-compose

Download the configuration

To download the configuration you can use Git:

1
2
git clone https://github.com/rinproduction/docker-rweb
cd docker-rweb/rweb

Note: I created a rweb sub-folder because Compose uses it to create individual Docker instance labels - so rename it carefully if you have to.

If you don’t have git, download the repository from this address: https://github.com/rinproduction/docker-rweb/archive/main.zip.

Activate the configuration

Premise: you must have permissions to access the docker service to do this. So either your user belongs to the docker group or you have to use administrative privileges (with su or sudo).

1
docker-compose up

If you haven’t received any errors, the service is active!

Point your browser to:

Security Notes

The code was written with the purpose of testing the possibility and ease of use of Docker Compose applied to our problem. In case you want to use it in production, depending on the situation, there may be other safety measures to be taken. Definitely you must regenerate the SSL key in the config/ssl/ directory as the secret key is shared on a public repository (for the benefit of dissemination). Find out how to regenerate self-signed certificates here.

Conclusion

With this practice you get a system isolated from the main server, whose internal parts are well separated from each other, can be updated individually and are maximally reproducible: it means that, tested on one server, in principle they work on all the others.

Comments

Comment on Linkedin

Comment on Twitter

Share on