chuck — A training tool for deploying Shiny Apps
Tags: rstats, shiny, r
Published: 2020-01-06 20:05:05

---
title: chuck — A training tool for deploying Shiny Apps
published: true
date: 2020-01-05 00:00:00 UTC
tags: rstats, shiny, r
canonical_url: https://colinfay.me/chuck-deployment-shiny-docker-kubernetes/
---

`chuck` is a small app you can use as a training tool for deployingShiny applications.

### Why?

In late December the ThinkR team followed a three day workshop onKubernetes, which was the opportunity for us to receive a propertraining on how to deploy apps to Kube. One challenging thing for thistraining was to find an app that was useful for toying around: the _OldFaithful Geyser Data_ app is nice when you start Shiny, but it lackswhat you’d expect a production Shiny app to have. For example, itdoesn’t have any external inputs, it isn’t plugged into any db, and ofcourse you don’t have any feedback about the context of the application(server information, R session info, etc.)

## And here comes `chuck`

`chuck` is a small, relatively funny Shiny app that contains somefeatures that can be used to train your skills when it comes todeploying Shiny apps with Docker and Kubernetes.

![](https://colinfay.me/assets/img/chuck1.png)

Let me describe the infrastructure of this app:

- This app takes a random “Chuck Norris Fact” from the _icndb_website, aka the Internet Chuck Norris Data Base[http://www.icndb.com/](http://www.icndb.com/), so the container launching your Shiny appneeds to have access to the internet.
- This app needs to be connected to a Mongo DB database, so you alsoneed to handle the access to this DB.
- Database info is printed to R so you have something to look for inthe Docker & Kube logs.
- As it relies on `{mongolite}`, it has system requirements.
- The connection parameters are passed through environment variables,so you can also play with these ones either in Docker, or in Kubewith Environment variables, configMap or Secrets.
- On the app, you can chose to “save” or “skip” when a joke israndomly shown. If you chose save, it’s saved inside the mongodatabase. You can see the number of elements registered in the mongocollection + the info about the db. That allows you to play withmongo collection and database name, storage systems (keep the dbcontent when the app / docker / pods are closed), etc.
- The “Show R Session” and “Server wtfismyip” respectively run`utils::sessionInfo()` and`jsonlite::read_json("https://wtfismyip.com/json")` and prints theoutput inside the modals, so you can retrieve info about the Rsession the app is run in, and about the location of the server.

![](https://colinfay.me/assets/img/chuck2.png)

## Find `chuck`

You can find chuck on my GitHub at: [https://github.com/ColinFay/chuck](https://github.com/ColinFay/chuck)

You’ll find the app in the `chuck/` folder. It’s a rather basic ShinyApp built with `{golem}` so nothing fancy here.

The `Dockerfile` at the root of the project is the one used to build thecontainer for [https://hub.docker.com/repository/docker/colinfay/chuck](https://hub.docker.com/repository/docker/colinfay/chuck).As you can see, it contains a series of environment variables which areused inside the Shiny App to connect to mongo. It defaults to serving onport 3838 but that can also be set by an env variable.

If you don’t want to go into too much trouble, you can simply run:

```
docker pull colinfay/chuck:0.2.0
docker pull mongo:3.4 

docker network create chucknet

docker run -v $(pwd)/db:/data/db -p 27017:27017 \
  -d --name mongo --net chucknet mongo:3.4

docker run -e MONGOPORT=27017 -e MONGOURL=mongo \
  -e MONGODB=pouet -e MONGOCOLLECTION=pouet -p 3838:3838 \
  --name chuck --net chucknet -d colinfay/chuck \
  && sleep 5 && open http://localhost:3838

```

In the `kube/` folder, you’ll find the YAMLs used during the training todeploy the app with minikube. If you’re in minikube right now, you’llprobably just have to `git clone` and `kubectl apply -f chuck/` to getthis running. This folder contains three yaml files for the mongodatabase: one for the deployment, one for the persistent volume claim,and one for the service.

The shiny app has more files: one for the deployment and one for theservice (classical), and also a configMap and a secret, which are mainlyused as an example for practicing these features. The ingress was thereto help exposing the app to the world during the training, but you mightwant to use other configs for your platform (for example, I had to useanother method for Google Cloud Engine).

The main goal is to have something to exercise your skills, so maybeyou’d want to write your own Dockerfile & kube YAMLs.

And remember,

> No statement can catch the ChuckNorrisException.