Search code examples
mysqlrdockershinygolem

R Docker: Can't connect to local MySQL server through socket


I've used a golem pipeline to package & dockerize my app.

For starters, I am trying to deploy the app locally on windows pc using docker (also tried to run it on linux with a same problem). The app collects the data from a local SQlite database also running on my pc (which will be similar once deployed on a server).

When I run the app as a package, app functions alright. But once I create a docker image & run it, the app launched but is unable to connect to my local sql database, returning this error: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")

Connection to the database inside app looks like this:

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

My docker file looks like this:

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()"

Thanks.


Solution

  • Here are the issues I can see:

    • You're using 127.0.0.1 as a host for your database. Once in the container, this address refers to the inner IP of the container, not the one from your host machine / another container. So your app can't access the host DB.

    • You haven't installed the drivers for MariaDB inside your container

    Here are solutions: