r - 将 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;
}
}
解决方案
推荐阅读
- ios - 无法使用 PluginError 初始化 Amplify:无法解码配置
- python - 是否有任何理由在 Pandas 的方法名称“read_csv”中保留扩展名“csv”?
- python - 在没有 Internet 访问权限的情况下使用 GRPC python 并为自定义 Linux 发行版安装 pip
- visual-studio-code - Visual Studio Code 扩展:将外部片段拖放到编辑器中
- c++ - STL 通用算法 - 范围元素类型到谓词参数类型的隐式转换
- python - 如何使用 Python 将多个 Excel 工作簿合并到一个工作簿中
- xaml - 无法关闭 Xamarin Forms 中单击事件的弹出窗口
- python - Python线性回归组合问题
- php - WooCommerce 的 Mailchimp:删除同步问题的订阅者标签
- amazon-s3 - AWS Glue 和 S3 访问点