首页 > 解决方案 > 将 docker 容器内的闪亮应用程序与 Linux 上的外部 mysql 数据库连接起来

问题描述

我创建了一个闪亮的应用程序,连接到我的 Mac OS Catalina 上运行的 Mysql 数据库。当我在 Rstudio 中运行该应用程序时,它运行良好。在本地完成后,我会将应用程序迁移到 Linux Ubuntu 上的云中。

当我创建此应用程序的 docker 映像时 - 只是应用程序,而不是 mysql 数据库 - 我运行容器,在浏览器(本地主机)上访问应用程序,它没有连接到数据库。

我的 app.R 或 dockerfile 连接到外部 mysql 数据库的正确配置是什么?

我正在使用的文件如下。感谢任何帮助。

===============APP.R================================ ========

## libraries

library(tidyverse)
library(shiny)
library(shinydashboard)
library(ggplot2)
library(DBI)


#shiny dashboard UI
ui <- dashboardPage(
    dashboardHeader(),
    dashboardSidebar(),
    dashboardBody(
        fluidPage(
            plotOutput("plot1")
        )
    )
)

#shiny dashboard server
server <- function(input, output,session) {
    
    #connection to database  
    orders <- dbConnect(RMySQL::MySQL(), user = 'root', password = '#####', 
                        dbname = 'Archenar',host='localhost')
    #check changes
    rigel <- reactivePoll(5000, session,checkFunc = function() {
        query2= "SELECT * FROM SENSOR"
        rs = dbSendQuery(orders,query2)
        dbFetch(rs)
    },
    
    valueFunc = function() {
        query2= "SELECT * FROM SENSOR"  
        rs = dbSendQuery(orders,query2)
        dbFetch(rs)
    }
    )

    #make plot  
    output$plot1 <- renderPlot({
        rigelp <- rigel() %>% as.data.frame()%>% group_by(category) %>% 
            summarize(v=sum(value1))
        ggplot(rigelp,aes(x=category,y=v)) + geom_bar(stat = "identity")
        
    })  
}

shinyApp(ui, server)

=============Dockerfile==================================== ====

# get shiny server plus tidyverse packages image
FROM rocker/shiny:latest
# system libraries of general use
RUN apt-get update && apt-get install -y \
    sudo \
    pandoc \
    pandoc-citeproc \
    libcurl4-gnutls-dev \
    libcairo2-dev \
    libmariadbd-dev \
    libxt-dev \
    libssl-dev \
    libssh2-1-dev
# install R packages required 
# (change it depending on the packages you need)
RUN R -e "install.packages('shiny',repos='http://cran.rstudio.com/')"
RUN R -e "install.packages('tidyverse',repos='http://cran.rstudio.com/')"
RUN R -e "install.packages('shinydashboard',repos='http://cran.rstudio.com/')"
RUN R -e "install.packages('DBI',repos='http://cran.rstudio.com/')"
RUN R -e "install.packages('RMySQL',repos='http://cran.rstudio.com/')"
# Copy configuration files into the Docker image
COPY shiny-server.conf  /etc/shiny-server/shiny-server.conf
COPY /app /srv/shiny-server/
RUN rm /srv/shiny-server/index.html
# Make the ShinyApp available at port 80
EXPOSE 3838
# Copy further configuration files into the Docker image
COPY shiny-server.sh /usr/bin/shiny-server.sh
RUN ["chmod", "+x", "/usr/bin/shiny-server.sh"]
CMD ["/usr/bin/shiny-server.sh"]

=========闪亮服务器======================================== =

# Define the user we should use when spawning R Shiny processes
  run_as shiny;
  # Define a top-level server which will listen on a port
  server {
    # Instruct this server to listen on port 80.
    listen 80;
  # Define the location available at the base URL
    location / {
  # Run this location in 'site_dir' mode, which hosts the entire directory
      # tree at '/srv/shiny-server'
      site_dir /srv/shiny-server;
      
      # Define where we should put the log files for this location
      log_dir /var/log/shiny-server;
      
      # Should we list the contents of a (non-Shiny-App) directory when the user 
      # visits the corresponding URL?
      directory_index on;
    }
  }

标签: rdockershiny

解决方案


正如@Miguel所建议的,您需要使用主机网络模式。

应用程序
docker run your-image --net=host

当您使用 Linux 机器时,您可以使用相同的机器。运行后访问您的应用程序--net=host

linux-machine-ip:3838

欲了解更多信息


推荐阅读