This creates a dedicated Docker container that has all the libraries, files and scripts necessary to run your Shiny app. This example uses a local
Dockerfile to install the libraries you need, but in addition also copies your Shiny app scripts so its all self-contained and portable.
The Shiny app can then be deployed on new instances.
docker_build()to create and push your Shiny image to Google container registry
Once built, you can deploy straight from the Container Registry, so not necessarily needing steps 1 and 2.
You can use build triggers from Google Container Registry to build the docker image.
This is typically done by pushing up to a GitHub repository with your Dockerfile, which triggers a build. You can then construct the name of this docker image directly using
gce_tag_container, for use in a Shiny templated
Dockerfile includes a
COPY command to copy necessary Shiny files such as
server.R into the Docker image.
FROM rocker/shiny MAINTAINER Mark Edmondson (email@example.com) # install R package dependencies RUN apt-get update && apt-get install -y \ libssl-dev \ ## clean up && apt-get clean \ && rm -rf /var/lib/apt/lists/ \ && rm -rf /tmp/downloaded_packages/ /tmp/*.rds ## Install packages from CRAN RUN install2.r --error \ -r 'http://cran.rstudio.com' \ googleAuthR \ && Rscript -e "devtools::install_github(c('MarkEdmondson1234/googleID')" \ ## clean up && rm -rf /tmp/downloaded_packages/ /tmp/*.rds ## assume shiny app is in build folder /shiny COPY ./shiny/ /srv/shiny-server/myapp/
COPY command copies from a folder in the same location as the
Dockerfile, and then places it within the
/srv/shiny-server/ folder which is the default location for Shiny apps. This location means that the Shiny app will be avialable at
The example Dockerfile above installs
googleAuthR from CRAN,
googleID from GitHub and a Debian dependency for
googleAuthR that is needed,
apt-get. Modify this for your own needs.
FROM field could be a previously made image you or someone else has already created, allowing you to layer on top. The above example is available via a public Google Continer Registry window, made for this purpose, which you can see here:
shiny-googleauthrdemo is the Dockerfile above - the name for this can be created via the
library(googleComputeEngineR) gce_tag_container("shiny-googleauthrdemo", project = "gcer-public")
##  "gcr.io/gcer-public/shiny-googleauthrdemo"
This can then be added to your Dockerfile:
FROM gcr.io/gcer-public/shiny-googleauthrdemo MAINTAINER Mark Edmondson (firstname.lastname@example.org) # install R package dependencies RUN apt-get update && apt-get install -y \ ##### ADD YOUR DEPENDENCIES ## clean up && apt-get clean \ && rm -rf /var/lib/apt/lists/ \ && rm -rf /tmp/downloaded_packages/ /tmp/*.rds ## Install packages from CRAN RUN install2.r --error \ -r 'http://cran.rstudio.com' \ ##### ADD YOUR CRAN PACKAGES ##### && Rscript -e "devtools::install_github( ## ADD YOUR GITHUB PACKAGES )" \ ## clean up && rm -rf /tmp/downloaded_packages/ /tmp/*.rds ## copy your shiny app folder below COPY ./shiny/ /srv/shiny-server/myapp/
Hopefully more images can be added in the future, along with community contributions. They are rebuilt every commit to the
googleComputeEngineR GitHub repo.
Once you have the Dockerfile, place it into a folder with this structure alongside your Shiny app:
| |- /appname/ | |- ui.R |- server.R | Dockerfile
The file structure for this build is then:
list.files(get_dockerfolder("shiny-googleAuthRdemo"), recursive = TRUE) # "Dockerfile" "shiny/DESCRIPTION" "shiny/readme.md" "shiny/server.R" "shiny/ui.R"
You have a few options here:
Building images may take 10mins or so, especially if its the first layer of the image.
Start up a Shiny templated image, which makes sure the right ports are open etc. but also supply the
dynamic_image argument pointing at the Docker image you have built in previous step.
## make new Shiny template VM for your self-contained Shiny app vm <- gce_vm("myapp", template = "shiny", predefined_type = "n1-standard-2", dynamic_image = gce_tag_container("custom-shiny-app", "your-project"))
Now you have a built app, you can deploy it to other instances simply by specifying the build shiny image. If using the recommended Build trigger method, you can specify development or production folders in your GitHub repository. You will need to restart a Shiny VM to load the latest build.
Clean up the VMs to avoid unnecessary costs:
# delete build VM gce_vm_delete(vm)