Connect to db in config.R

I have a quite large shiny application in which some constant data is loaded from db (mysql db on external server) in config.R before running of Shiny application. It works when I run docker container only, but in shinyproxy I receive the error “Container did not respond…”
But when I delete these db queries in config.R and load it by another way, then the application works, and afterthat all db queries perform well.

I really appreciate any help

My application configuration

- id: docker_app
    display-name: App
    description: My app
    container-image: hrychaniuk/app:latest
    network_mode: "host"
    container-volumes: ["/var/run/mysqld/mysqld.sock:/var/run/mysqld/mysqld.sock"]

Hi Vanhry,

few things you should check. Log of container started by shinyproxy (docker ps to get container id and then docker container logs --follow ). If container dies fast you will have a problem with this method. Also, check shinyproxy log file.

But, I do not understand what you do with config.R. Actually how you start it in container first and shiny app after that? What is CMD command in your Dockerfile?

Also, when you start container only, do you map mysql socket like you do in shinyproxy config? I have never used database over socket but over db library and tcp…

Command network_mode is for docker swarm as backend, and than network things became more complicate… Do you use docker as backend od docker swarm?

Hi Dingo,

Thank you for a quick reply

In the logs of container appears only (except regular library loading stuff) this warning message

System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to create bus connection: Host is down

I will get rid of the network_mode and container-volumes, I added it when I was trying to fix the problem

About config.R: No, There is the regular shinyApp call shiny::runApp('/root/docker_app', port=3838, host='0.0.0.0'), but I have source("config.R") in both server and ui.
Maybe the error occurred because the constant data which is used by server/ui has not been loaded from DB yet when the application starts.
Or docker is not able to connect to db when the application starts (because after application loading all db queries work well)

About warning, I have no idea. Maybe someone else can help. What is your method for starting docker (eg, direct packet installation or maybe over snap on ubuntu)? Check if dockerd is listening over tcp:
ps -aux | grep dockerd
and if you do not see something like
root 780 0.0 3.0 1160296 123304 ? Ssl Aug09 4:15 /usr/bin/dockerd -H unix:// -D -H tcp://127.0.0.1:2375
than try to fix it with this manual: https://www.atlantic.net/vps-hosting/how-to-set-up-remote-access-to-docker-daemon/
Have you tried to run any test app on that shinyproxy installation?

About config.R: I understand now. Maybe it is not good idea to inline that file to both ui.R and server.R. It is true that you can not guarantee which file will be first runned, but maybe this double connection making some problems. I think that you need to see console output - like I wrote to you in my first reply. Than you will find out what is happening inside app.

I was running shinyproxy with testing applications, and it worked, the only problem that causes shinyproxy to fail is when there are db queries in the config.
Maybe if I don’t find the solution I will just remove the queries from config.R and try to find some another way.

Thank you very much for your help.

Vanhry,

I do not have complete insight in all your setup files but judging to all what we have wrote here I had to ask you did you tried to remove 'network_mode: “host” '?

It looks to me that your container can not access DB over network.

It didn’t help me, but I agree with you that problem is accessing DB over network.
When I run docker in bash, I am able to make queries to db.
I tried to pass these credentials to container-env-file but it didn’t help me too (like these)

MYSQL_PASSWORD=password
MYSQL_DATABASE=db
MYSQL_USER=user
MYSQL_HOST=12.345.678.910

My application.yml

proxy:
  container-wait-time: 20000
  title: ShinyProxy
#  logo-url: https://link/to/your/logo.png
  landing-page: /app/docker_app
  favicon-path: favicon.ico
  heartbeat-rate: 10000
  heartbeat-timeout: 600000
  port: 8080
  hide-navbar: true
  authentication: none
  # Docker configuration
  docker:
    cert-path: /home/none
    url: http://127.0.0.1:2375
    port-range-start: 20000
  specs:
  - id: docker_app
    display-name: APP
    description: App
    container-image: hrychaniuk/app:latest
    container-env-file: /root/docker_app/docker-env.yml
    logo-url: https://github.com/analythium/shinyproxy-1-click/raw/master/digitalocean/images/app-hist.png
 
logging:
  file:
    shinyproxy.log

spring:
  servlet:
    multipart:
      max-file-size: 3000MB
      max-request-size: 200MB

My Dockerfile

FROM openanalytics/r-base


# update packages, add the new ones for r 4.0
RUN apt-get update && apt-get install -y --no-install-recommends software-properties-common && add-apt-repository ppa:c2d4u.team/c2d4u4.0+

# install system dependencies for R packages
RUN apt-get update && \
                                      apt-get install -y --no-install-recommends \
                                      ...
# linux dependencies here .....


# Download and install library
RUN R -e "install.packages(c(...packages here...))"

RUN mkdir -p /root/docker_app
COPY app /root/docker_app
COPY /app/RProfile.site/ /usr/lib/R/etc/

EXPOSE 3838
CMD ["R", "-e", "shiny::runApp('/root/docker_app', port=3838, host='0.0.0.0')"]

What is purpose of this in Dockerfile:
port=3838, host='0.0.0.0')

Also, for container-env-file I saw for the first time.

Try to call container with minimal CMD as it is in all manuals:

["R", "-e", "shiny::runApp('/root/docker_app')"]

And try to follow console output (docker logs --follow <container_id>).

I just tried to specify explicitly the port and host, removing them changed nothing.
There are no errors in the console output.

Well,
it can be how you access DB. Using which library… I am not a experienced R programmer…
Is there any sign of accessing in DB log? Is there locking access by IP addresses in My Sql?

I’ve found the solution.
As I run quite a large app on localhost, container stop waiting more quickly than the application was loaded.
I increased the container-wait-time to container-wait-time: 40000
Also I leave the only one source("config.R") call in project (in server.R).

Thank you very much for your help!

1 Like

Hi @vanhr

I’m getting a similar error, but with a slightly different setup - what did your end working Dockerfile and application.yml look like?

Hi!
My solution was increasing container-wait-time because I had a large application that took too much time for initialization. My application.yml and Dockerfile are the same as above, they are correct

thanks.

Very helpful.

Still haven’t sorted out my own issue but seeing your setup helped with ruling out potential locations of the error for me.