I am attempting to create a Dockerized Flexdashboard app. I am able to run it locally, but not in the localhost. I can, however, run shiny apps just fine within the same docker image. Interestingly, the error message in the localhost says 'Not Found' for the Flexdashboard app which is different than when a page doesn't exist at all ('Page Not Found').
How can I Dockerize a Flexdashboard app?
dir.create("testshinydocker")
dir.create("testshinydocker/apps")
dir.create("testshinydocker/apps/kmeans")
dir.create("testshinydocker/apps/kmeansflex")
cat(readLines("https://raw.githubusercontent.com/rstudio/shiny-examples/master/050-kmeans-example/server.R"),
file = "testshinydocker/apps/kmeans/server.R", sep = "\n")
cat(readLines("https://raw.githubusercontent.com/rstudio/shiny-examples/master/050-kmeans-example/ui.R"),
file = "testshinydocker/apps/kmeans/ui.R", sep = "\n")
cat(
c('FROM rocker/shiny:latest\n',
"RUN echo 'install.packages(c(\"flexdashboard\"), \\",
"repos='$MRAN', \\",
"dependencies=TRUE)' > /tmp/packages.R \\",
" && Rscript /tmp/packages.R\n",
'EXPOSE 3838\n',
'COPY apps /srv/shiny-server/\n',
'CMD ["/usr/bin/shiny-server.sh"]\n'),
file = "testshinydocker/Dockerfile",
sep = "\n"
)
cat(readLines("https://raw.githubusercontent.com/rstudio/flexdashboard/master/examples/11_shiny-kmeans-clustering/dashboard.Rmd"),
file = "testshinydocker/apps/kmeansflex/kmeans2.Rmd", sep = "\n")
shiny::runApp('testshinydocker/apps/kmeans')
rmarkdown::run("testshinydocker/apps/kmeansflex/kmeans2.Rmd")
cd {path to testshinydocker directory}
docker build -t myapp .
docker run --rm -d -p 3838:3838 myapp
This is likely a result of a bug in the rmarkdown package being used in your image (rmoarkdown v. 1.18) and is related to this: https://github.com/rstudio/rmarkdown/issues/1731 and https://github.com/rstudio/rmarkdown/issues/1714. I'm guessing http://localhost:3838/kmeansflex/kmeans2.Rmd does indeed work.
Here's how you could test this. Make a shiny app in the apps folder to see what version of rmarkdown is being run. Make a folder in apps called 'rmarkdown'. Then put the following simple ui.R and server.R scripts in there to build a shiny app (we know shiny apps render for you) to determine what version of rmarkdown you have:
fluidPage(
hr(),
fluidRow(column(3, verbatimTextOutput("value")))
)
function(input, output) {
output$value <- renderPrint({ utils::packageVersion('rmarkdown') })
}
Then docker build
and re-docker run
and go to http://localhost:3838/rmarkdown/ in the browser. It should take you to the screen and display the version of rmarkdown you have. If it is 1.18 that's the culprit.
If the rmarkdown installed is version 1.18 a possible solution would be to install rmarkdown from github in your Dockerfile so that you'd get a newer version without this bug and all is well. Here's what that would look like in your Dockerfile:
FROM rocker/shiny:latest
# apt-get and system utilities
RUN apt-get update && apt-get install -y \
libssl-dev \
libsodium-dev
RUN echo 'install.packages(c("flexdashboard", "remotes", "openssl"), \
repos='$MRAN', \
dependencies=TRUE)' > /tmp/packages.R \
&& Rscript /tmp/packages.R
RUN Rscript -e 'remotes::install_github("rstudio/rmarkdown")'
COPY apps /srv/shiny-server/
EXPOSE 3838
CMD ["/usr/bin/shiny-server.sh"]