The Zühlke Shiny Docker Image
Running the Shiny Server
The command for a simple smoke test is:
docker run --rm -p 80:3838 zuehlke/shiny
This will start the server on port 80. If you run Shiny for the first time docker will download some images from the internet and this will take some time. Besides the download
messages there won't be any console output and one might think that the system is hanging.
You can access Shiny in your browser:
Please notice the
-p argument. Inside the Docker image shiny is running on port 3838. With the
-p argument we expose it on port 80 of the local machine (or the local VM in case of
Windows and boot2docker).
Application have to be placed at a specific location in the Docker image. Shiny will look for applications at
/srv/shiny-server. There are several ways to deploy our application to
that location but the easiest and most generic approach is by mounting a volume. While we are at it we also mount a second volumne that contains the logfiles of the server (optional).
We mount volumns with the
-v argument. The syntax is
/path/on/local/drive/:/path/in/docker/image/. If you're working on Windows with boot2docker the command will look something like this:
docker run --rm -p 80:3838 \ -v /c/Users/xxx/Documents/myshinyapps/:/srv/shiny-server/ \ -v /c/Users/xxx/Documents/myshinylogs/:/var/log/ \ zuehlke/shiny
If you reload the browser now you should get a list of deployed applications.
Build the default Zühlke Shiny App-Container
- (Windows) Start boot2docker
- (Linux) open a terminal
- In the terminal window, change into the directory of this file that you're currently reading
- Build the image:
- Run the script
./build.sh(this will take some time!)
- This will create the image
zuehlke/shiny:latest. Basically this scripts simply executes the command
docker build --rm=true -t zuehlke/shiny .
- Run the script
- Now, the image has been loaded into your local docker repository and you can use it with the
Extending the default Shiny App
If you need additional R packages that are only used by your specific application you have to create a new Docker image based on the default Zühlke Shiny image. If we plan to use the
new package for all or many of future Zühlke projects you can modify the default Zühlke Shiny Docker image and create a new release of it.
For a new Docker image do the following:
- Create a new Directory/Project
- Create a file
Dockerfilein the new directory
- The first line of the
- You can replace
latestwith a specific version of the base image if needed.
- You can replace
- Add additional commands to the
Dockerfilethat install the additional packages (see next chapter for details).
- Build the new Docker image:
docker build --rm=true -t "zuehlke/myapp" .
Additional APT packages
To install additional apt packages add a RUN command like in the following example
RUN apt-get install -y \ texlive-base
Additional R packages
Additional R packages can be installed in two ways. One is for the CRAN packages add one is
for packages that are hosted on github.
Adding additional CRAN R packages
RUN command containing the package to install. You can put multiple package names into the
RUN R -e "install.packages(c('devtools'), repos='$CRAN_REPO')" RUN R -e "install.packages(c('curl', 'XML'), repos='$CRAN_REPO')"
Adding additional Github hosted R packages
RUN command containing the github user and repo name (user/repo). You can put multiple repos into the
RUN R -e "devtools::install_github(c('rstudio/shinydashboard'))" RUN R -e "devtools::install_github(c('hadley/shinySignals', 'jcheng5/bubbles', 'rstudio/leaflet'))"
Debuging: Looking inside the container
Sometime you would like to see inside the container after you created it. You can run a new container and provide an alternative command that get's executed at startup.
The default command will start shiny but we can simply call
/bin/bash instead. To make things work we have to tell docker that we want to run in the interactive mode with the flags
./run-interactive.sh will do this for you. Please note that when using this script there won't be a running server and no applicaitons will be mounted.
If you want to see the deployed application on the file system you have to enter the whole docker command. It will look something like this:
docker run --rm -p 80:3838 -it \ -v /c/Users/xxx/Documents/myshinyapps/:/srv/shiny-server/ \ -v /c/Users/xxx/Documents/myshinylogs/:/var/log/ \ zuehlke/shiny /bin/bash
Please note that we still don't have a running server. You'll find yourself inside the Zühlke Shiny container but without a running Shiny server! If you want to start Shiny during your debug session you can
/usr/bin/shiny-server.sh inside the container.
You can leave the container by calling
exit. This will destroy the whole container!