I'm making a R shiny app, which calls the database (SQL Server) using DBI and odbc and displays a table. My app works perfectly on my local machine. However, when I tried to dockerize the shiny app, it just failed to start. No clue why that does not work.
I feel like I might need to install the odbc drive in my dockerfile. Maybe I'm also missing something else...
I'm new to docker. Appreciate it if someone can show how I can modify my dockerfile so that my shiny app also works as docker container.
below is my dockerfile:
# Base image https://hub.docker.com/u/rocker/
FROM rocker/shiny:latest
# system libraries of general use
RUN apt-get update -qq && apt-get -y --no-install-recommends install \
libxml2-dev \
libcairo2-dev \
libsqlite3-dev \
libmariadbd-dev \
libpq-dev \
libssh2-1-dev \
unixodbc-dev \
libcurl4-openssl-dev \
libssl-dev
## update system libraries
RUN apt-get update && \
apt-get upgrade -y && \
apt-get clean
# Install R packages that are required
RUN R -e "install.packages(c('dplyr','DT', 'DBI', 'odbc'), 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/
# Make the ShinyApp available at port 80
EXPOSE 80
# Copy further configuration files into the Docker image
COPY shiny-server.sh /usr/bin/shiny-server.sh
CMD ["/usr/bin/shiny-server.sh"]
below is my shiny ui.r:
ui <- fluidPage(
titlePanel("Database table"),
sidebarLayout(
sidebarPanel(),
mainPanel(
fluidRow (
column(12, DT::dataTableOutput('datatable')) )
)
)
)
my server.r
server <- function(input, output, session) {
conn <- DBI::dbConnect(odbc::odbc(),
Driver = "ODBC Driver 17 for SQL Server",
Server = 'xxxxxx',
Database = "xxxxxx",
UID = "xxxxxx",
PWD = 'xxxxxxx',
port=1433)
df = dbGetQuery(conn, "SELECT * FROM myTable")
dbDisconnect(conn)
output$datatable <- DT::renderDataTable({
DT::datatable(df)
})
}
global.r:
library(shiny)
library(DBI)
library(odbc)
library(dplyr)
library(DT)