如何在Docker容器中运行R Shiny App [英] How to run R Shiny App in Docker Container

查看:440
本文介绍了如何在Docker容器中运行R Shiny App的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为R Shiny App构建了Docker映像,并在Windows 10 Home上使用Docker Toolbox运行了相应的容器。尝试使用我的网络浏览器打开该应用程序时,仅显示索引。我不知道为什么不执行该应用程序。

I built a Docker Image for an R Shiny App and ran the corresponding container with Docker Toolbox on Windows 10 Home. When trying to open the App with my web browser, only the index is shown. I don't know why the app isn't executed.

日志向我显示以下内容:

The log shows me this:

*** warning - no files are being watched ***
[2019-08-12T15:34:42.688] [INFO] shiny-server - Shiny Server v1.5.12.1 (Node.js v10.15.3)
[2019-08-12T15:34:42.704] [INFO] shiny-server - Using config file "/etc/shiny-server/shiny-server.conf"
[2019-08-12T15:34:43.100] [INFO] shiny-server - Starting listener on http://[::]:3838

上启动侦听器

我已经通过执行以下命令来指定应用程序主机到容器的路径,该命令引用了Docker集线器映像:

I already specified the app host-to-container path by executing the following command which refers to a docker hub image:

docker run --rm -p 3838:3838 -v /C/Docker/App/:/srv/shinyserver/ -v /C/Docker/shinylog:/var/log/shiny-server/  didsh123/ps_app:heatmap

我的Docker File如下所示:

My Docker File looks like the following:

# get shiny serves plus tidyverse packages image
FROM rocker/shiny-verse:latest

# system libraries of general use
RUN apt-get update && apt-get install -y \
    sudo \
    pandoc \
    pandoc-citeproc \
    libcurl4-gnutls-dev \
    libcairo2-dev \
    libxt-dev \
    libssl-dev \
    libssh2-1-dev

##Install R packages that are required--> were already succesfull
RUN R -e "install.packages(c('shinydashboard','shiny', 'plotly', 'dplyr', 'magrittr'))"

#Heatmap related packages
RUN R -e "install.packages('gpclib', type='source')"
RUN R -e "install.packages('rgeos', type='source')"
RUN R -e "install.packages('rgdal', type='source')"

# copy app to image
COPY ./App /srv/shiny-server/App

# add .conf file to image/container to preserve log file
COPY ./shiny-server.conf  /etc/shiny-server/shiny-server.conf


##When run image and create a container, this container will listen on port 3838
EXPOSE 3838

###Avoiding running as root --> run container as user instead
# allow permission
RUN sudo chown -R shiny:shiny /srv/shiny-server
# execute in the following as user --> imortant to give permission before that step
USER shiny

##run app
CMD ["/usr/bin/shiny-server.sh"]

因此,当我在浏览器中处理docker ip和评估的端口时,该应用程序应在此运行,但仅显示索引。我使用以下行:

So when I address the docker ip and the assessed port in the browser, the app should run there, but only the index is displayed. I use the following line:

http://192.168.99.100:3838/App/

我很高兴收到任何提示或建议。我是Docker的新手,所以我也乐于提供详细的说明。

I'm glad for every hint or advice. I'm new to Docker, so I'm also happy for detailed explanations.

推荐答案

要在Docker中使用Shiny,建议您可以使用 golem 软件包。 golem提供了构建闪亮应用程序的框架。如果您有根据其框架开发的应用程序,则功能 golem :: add_dockerfile()可用于自动创建dockerfile。

To use shiny with docker, I suggest you use the golem package. golem provides a framework for builing shiny apps. If you have an app developed according to their framework, the function golem::add_dockerfile() can be used to create dockerfiles automatically.

如果您对框架不感兴趣,仍然可以查看 add_dockerfile()的来源看看他们如何管理部署。他们的策略是将 shiny :: runApp()与port参数一起使用。因此,在这种情况下,不需要 shiny-server

If you are not interested in a framework, You can still have a look at the source for add_dockerfile() to see how they manage the deployment. Their strategy is to use shiny::runApp() with the port argument. Therefore, shiny-server is not necessary in this case.

golem中的Dockerfile看起来大致像这样

The Dockerfile in golem looks roughly like this

FROM rocker/tidyverse:3.6.1
RUN R -e 'install.packages("shiny")'
COPY app.R /app.R
EXPOSE 3838
CMD R -e 'shiny::runApp("app.R", port = 3838, host = "0.0.0.0")'

这将使该应用程序在端口 3838上可用。当然,您将必须安装任何其他R软件包和系统依赖项。

This will make the app available on port 3838. Of course, you will have to install any other R packages and system dependencies.


  • 为提高可重复性,建议您使用 remotes :: install_version()代替 install.packages()

  • 如果您要部署具有相似依赖性的多个应用程序(例如 shinydashboard ),则编写您自己的基本图像,可以用来代替 rocker / tidyverse:3.6.1 。这样,您的构建将更快。

  • To increase reproducibility, I would suggest you use remotes::install_version() instead of install.packages().
  • If you are going to deploy several applications with similar dependencies (for example shinydashboard), it makes sense to write your own base image that can be used in place of rocker/tidyverse:3.6.1. This way, your builds will be much quicker.

这篇关于如何在Docker容器中运行R Shiny App的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆