Deploying a Shiny app in a Docker container

Pablo Leon-Rodenas|Data and Analytics|NHSE|RPySOC24

November 22, 2024

Introduction

  • This presentations shows a Shiny app deployed inside a Docker Container in a Debian desktop similar to Raspberry PI OS

Main topics

  • What is a Docker Container?

  • Design and build your Shiny App

  • Create a Dockerfile

  • Build your adhoc Docker image from Dockerfile

  • Deploy Shiny app to a Docker Container

What is Docker?

A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another.

A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings. https://www.docker.com/

Container images become containers at runtime and in the case of Docker containers – images become containers when they run on Docker Engine.

Available for both Linux and Windows-based applications, containerized software will always run the same, regardless of the infrastructure.

Containers isolate software from its environment and ensure that it works uniformly between development and staging.

https://www.docker.com/resources/what-container/

The Rocker Project

I am using Rocker Project Docker container image. A specific Docker Container for the R Environment. This allows us to start R inside a Container

Get rocker/r-base Docker image

Run Docker image to start R inside a container

Shiny App to be deployed in Docker

This Shiny app displays COVID 19 Confirmed, Recovered and Death cases by country for two months period. I have used renv to restore entire project after snapshot taken.

1. Pull image and build Dockerfile

Pull required rocker/r-base Docker image to build your own.

Actions in the Docker.txt file

  • Base image on rocker/r-base

  • Replicate local folder structure in Dockerfile

  • Copy files into Dockerfile

  • Run R scripts in Docker when buildinfg the image

Docker file is a text file we create in the project directory

https://github.com/Pablo-source/RPYSOC_2024_Docker_Shiny/blob/ main/Docker.txt

2. Build our Docker image from Dockerfile

Then build a specific image based on our Dockerfile. Putting together all components for our own Docker image.

The process of building the Docker image can take a while, we are re-creating the environment and installing all packages on top of original rocker/r-base Docker image we obtained from https://hub.docker.com/_/r-base

3. Build your image and bundle your code

We use docker build command at the project folder directory level. Where our Dockerfile is saved.

Use “docker image ls” command to display docker image we just built IMAGE ID: 67f74374b225

You can modify this docker image you just built and re-build it again after several changes:

docker build -t myname/myimage .

4. Spin a container running your app

Once the Docker image has been built, then you can use it to start in an brand new container in Docker.

docker run -d –rm -p 3838:3838 my-shinyapp-image

5. Share your Docker file

Finally use docker push command to share your Docker image with the community of Docker users in Docker Hub.

docker push

This is Docker Hub website to share your Docker file and apps

Browse for Docker files with apps in Docker Hub https://hub.docker.com/

THANKS !