Turning an R Shiny apps into a Docker image

As of now, SciLifeLab Serve only allows to host R Shiny applications if they are packaged as Docker images. While this is a straightforward process, it can cause difficulties if you have never created a Docker image before. To make this easier, the SciLifeLab Serve team is happy to help you with this through a free individual consultation, get in touch with us by e-mailing datacentre@scilifelab.se. Alternatively, we provide step-by-step instructions on how to do it on this page. We do not go in detail explaining what each of the tools and settings we use here mean as we try to keep it as simple/short as possible. You can learn more about Docker images in the official Docker documentation.

You can find the example Shiny app used in this tutorial on GitHub.

Preparation: install Docker and create an account on DockerHub

In order to build an image you need to make sure you have Docker installed. In addition, to publish this image you will need to create an account on DockerHub. You can log in to Docker Deskhop using these credentials and you will use the username when building the image.

Step one: create a Dockerfile

Docker images are built from sets of instructions given in a so-called Dockerfile. The name of the file should be exactly 'Dockerfile' and it should not have any file extension. We created an example Dockerfile to make it easier to start. Create a file using any text editor you have and insert the code below or simply download this example from here.


                                        FROM rocker/shiny:latest

                                        RUN apt-get update && \
                                            apt-get upgrade -y && \
                                            apt-get clean && \
                                            apt-get install -y git libxml2-dev libmagick++-dev && \
                                            rm -rf /var/lib/apt/lists/*

                                        RUN Rscript -e 'install.packages(c("shiny","tidyverse"))'

                                        COPY /app/ /app/

                                        RUN cd /app/ && \
                                            sudo chown -R shiny:shiny /app/

                                        EXPOSE 3838

                                        CMD ["R", "-e", "shiny::runApp('/app/', host = '0.0.0.0', port = 3838)"]

                                        

Let us look at how you can customize it for your own app. In this example two packages were installed: shiny and tidyverse. This was done in the line RUN Rscript -e 'install.packages(c("shiny","tidyverse"))'. Add your own packages here as needed by your app.

In this example the actual Shiny app code (the file app.R) as well as all dependencies are located in the folder called 'app'. This folder as a whole is copied into the image in the line COPY /app/ /app/. Change the folder names in this line and lines below if you have your own folder name. NB: This Dockerfile is expected to be placed in the parent folder of your app folder (so you should have 'app' folder and this Dockerfile in the same folder).

Step two: build an image

Ensure that Docker Desktop is running. Open the Terminal (or Windows Terminal) on your computer and navigate to the folder where your app folder and Dockerfile are located. To build an image you will need to run the following command: docker build -t <your-user-name>/<your-app-name> . (NB: do not forget the dot at the end of the command). Before you run it, replace <your-user-name>/<your-app-name> with your DockerHub username and the name of the app. Building the image may take a while. Once the process is completed, you should be able to see the image among your images in the Docker Desktop app.

Step three: upload the Docker image into DockerHub

The easiest way to publish an image on DockerHub is to sign in to your account with Docker Desktop and then pick "Push to Hub" among the options for your app image. This will publish your image on https://hub.docker.com/r/<your-user-name>/<your-app-name>. For example, our example app is available on https://hub.docker.com/r/scilifelabdatacentre/shiny-adhd-medication-sweden.

Step four: publish your app on SciLifeLab Serve

Now that you built an image and made it available through DockerHub, you can follow our general instructions for publishing your R Shiny app on SciLifeLab Serve to make it available for others to see and interact with. When creating your app on SciLifeLab Serve, under Docker image you will need to give <your-user-name>/<your-app-name>; under path to folder insert / ; under app port insert 3838.