1

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.

Sébastien Rochette
  • 5,901
  • 2
  • 14
  • 39
ayasugihada
  • 151
  • 1
  • 9

1 Answers1

3

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:

Colin FAY
  • 3,744
  • 1
  • 8
  • 24
  • Thank you Colin. You are a Hadley Wickham of R deployment :). I will go with your suggested pipeline. Two more questions: 1. So is dockerizing sql database advised for R in production? I can go either way as far as I can see. I wanted to avoid it at first, because it seemed like an unnecessary overhead. 2. do I use r-db instead of rocker/tidyverse? Am I missing something using it? Thanks again! – ayasugihada Nov 04 '19 at 13:58
  • Also, there might be some problem with r-db tags. docker pull results in : Error response from daemon: manifest for colinfay/r-db:latest not found: manifest unknown: manifest unknown. Specifying to latest does not cut it. – ayasugihada Nov 04 '19 at 14:18
  • I'd say yes, dockerizing the SQL database can be best in production, but that very much depends on the context (for example, if the db already exists, you will plug on this one, not on a new docker container). If you're starting your deployment env from scratch yes, it's better to go for a dockerized solution (easier to install and to maintain). Note though that you'll have to share a volume between your host and the docker for data persistance. – Colin FAY Nov 04 '19 at 14:28
  • Regarding your other question, yes, use `r-db` insead of rocker/tidyverse. You have to specify the version, there is no `latest` right now (so `docker pull colinfay/r-db:3.6.1` or `3.6.0`), sorry it's not specified in the doc. https://cloud.docker.com/repository/docker/colinfay/r-db/tags – Colin FAY Nov 04 '19 at 14:31
  • It seems to me like it would be best to use docker compose to describe relations between app and db (simplest example I found is here https://github.com/stavshamir/docker-tutorial ). But I am still a bit ignorant about the usage of mysql withing the app docker (file). Using just "FROM colinfay/r-db:3.6.1" in the docker file results in the same error message "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")". I'd expect a different error instead. – ayasugihada Nov 05 '19 at 12:10