1

我使用 golem 管道来打包和 dockerize 我的应用程序。

对于初学者,我正在尝试使用 docker 在 windows pc 上本地部署应用程序(也尝试在 linux 上运行它,但遇到了同样的问题)。该应用程序从也在我的电脑上运行的本地 SQlite 数据库收集数据(一旦部署在服务器上,这将是类似的)。

当我将应用程序作为包运行时,应用程序功能正常。但是一旦我创建了一个 docker 映像并运行它,应用程序启动但无法连接到我的本地 sql 数据库,返回此错误: 无法通过套接字连接到本地 MySQL 服务器'/var/run/mysqld/mysqld.sock ' (2 "没有这样的文件或目录")

与应用程序内部数据库的连接如下所示:

    con =  dbConnect(RMariaDB::MariaDB(), dbname = "training_dash_db", user = "root", password = "", host = '127.0.0.1')

我的 docker 文件如下所示:

FROM rocker/tidyverse:3.5.3
RUN R -e 'install.packages("remotes")'
RUN R -e 'remotes::install_github("r-lib/remotes", ref = "97bbf81")'
RUN R -e 'remotes::install_cran("shiny")'
RUN R -e 'remotes::install_github("Thinkr-open/golem")'
RUN R -e 'remotes::install_cran("processx")'
RUN R -e 'remotes::install_cran("attempt")'
RUN R -e 'remotes::install_cran("DT")'
RUN R -e 'remotes::install_cran("glue")'
RUN R -e 'remotes::install_cran("htmltools")'
RUN R -e 'remotes::install_cran("shinydashboard")'
RUN R -e 'remotes::install_cran("shinydashboardPlus")'
RUN R -e 'remotes::install_cran("lubridate")'
RUN R -e 'remotes::install_cran("dplyr")'
RUN R -e 'remotes::install_cran("purrr")'
RUN R -e 'remotes::install_cran("plotly")'
RUN R -e 'remotes::install_cran("DBI")'
RUN R -e 'remotes::install_cran("tibbletime")'
RUN R -e 'remotes::install_cran("tsibble")'
RUN R -e 'remotes::install_cran("shinyWidgets")'
RUN R -e 'remotes::install_cran("leaflet")'
RUN R -e 'remotes::install_cran("pool")'
RUN R -e 'remotes::install_cran("RMariaDB")'
RUN R -e 'remotes::install_cran("roxygen2")'
COPY K2dashboard_*.tar.gz /app.tar.gz
RUN R -e 'remotes::install_local("/app.tar.gz")'
EXPOSE 80
EXPOSE 3306
CMD R -e "options('shiny.port'=80,shiny.host='0.0.0.0');K2dashboard::run_app()"

谢谢。

4

1 回答 1

3

以下是我可以看到的问题:

  • 您使用 127.0.0.1 作为数据库的主机。一旦进入容器,此地址指的是容器的内部 IP,而不是来自您的主机/另一个容器的 IP。因此您的应用无法访问主机数据库。

  • 您尚未在容器中安装 MariaDB 的驱动程序

以下是解决方案:

于 2019-11-04T13:40:31.427 回答