Shiny Docker Unable to Connect to API/Internet - r

I've setup a Shiny App using docker shiny image and all works well until I attempt to call API using httr package. From what I can get I'm unable to access internet from the App as I get the Timeout message from multiple sites, and verified the work from going through web browser. Wondering what setting I need to change?
From the RStudio Console within the App:
> library(httr)
> r <- GET("http://httpbin.org/get")
Error in curl::curl_fetch_memory(url, handle = handle) :
Timeout was reached: [httpbin.org] Connection timed out after 10000 milliseconds
Here is my DockerFile:
FROM rocker/tidyverse
RUN apt-get update
RUN apt-get install -y libpq-dev
RUN R -e 'install.packages("RPostgres")'
RUN R -e 'install.packages("DT")'

I just needed to add the following libraries in Dockerfile and worked
RUN apt-get install -y libpq-dev \
libssl-dev \
libcurl4-openssl-dev \
curl \
sudo \
pandoc \
pandoc-citeproc \
libcairo2-dev \
libxt-dev \
libssl-dev \
libssh2-1-dev

Related

Copying R Libraries to Docker Image from Local Libraries

I created a renv for my Shiny app. As you know that when you build a dockerfile, it has to install every package and its dependencies. If there are many packages, it takes long time.
I already installed packages my local machine. Also, the app will run on the remote machine. Is there any chance to copy the all libraries without restoring the renv? My local machine is macOS and my remote machine is Ubuntu. Due to the different Operating Systems might not work to use libraries. Probably, there are different dependencies between macOS and Ubuntu for the R packages.
Which is the best way to install R packages in a Dockerfile? Should I wait long docker building every time?
Directory
FROM openanalytics/r-base
# system libraries of general use
RUN apt-get update && apt-get install -y \
sudo \
nano \
automake \
pandoc \
pandoc-citeproc \
libcurl4-openssl-dev \
libcairo2-dev \
libxt-dev \
libssl-dev \
libssh2-1-dev \
libssl1.1 \
libmpfr-dev \
# RPostgres
libpq-dev \
libssl-dev \
# rvest
libxml2-dev \
libssl-dev
# Install Packages
# RUN R -e "install.packages(c('janitor','DBI','RPostgres','httr', 'rvest','xml2', 'shiny', 'rmarkdown', 'tidyverse', 'lubridate','shinyWidgets', 'shinyjs', 'shinycssloaders', 'openxlsx', 'DT'), repos='https://cloud.r-project.org/')"
# copy the app to the image
RUN mkdir /root/appfile
COPY appfile /root/appfile
# renv restore
RUN Rscript -e 'install.packages("renv")'
RUN Rscript -e "renv::restore(lockfile = '/root/appfile/renv.lock')"
EXPOSE 3838
CMD ["R", "-e", "shiny::runApp('/root/appfile')"]
Thanks.

Update SSL Certificates inside dockerfile

I have the following dockerfile:
FROM rocker/tidyverse:3.5.2
RUN apt-get update
# System dependices for R packages
RUN apt-get install -y \
git \
make \
curl \
libcurl4-openssl-dev \
libssl-dev \
pandoc \
libxml2-dev \
unixodbc \
libsodium-dev \
tzdata
# Clean up package installations
RUN apt-get clean
# ODBC system dependencies
RUN apt-get install -y gnupg apt-transport-https
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN curl https://packages.microsoft.com/config/debian/9/prod.list > /etc/apt/sources.list.d/mssql-release.list
RUN apt-get update
RUN ACCEPT_EULA=Y apt-get install msodbcsql17 -y
# Install renv (package management)
ENV RENV_VERSION 0.11.0
RUN R -e "install.packages('remotes', repos = c(CRAN = 'https://cloud.r-project.org'))"
RUN R -e "remotes::install_github('rstudio/renv#${RENV_VERSION}')"
# Specify USER for rstudio session
ENV USER rstudio
COPY ./renv.lock /renv/tmp/renv.lock
WORKDIR /renv/tmp
RUN R -e 'renv::consent(provided = TRUE)'
RUN R -e "renv::restore()"
WORKDIR /home/$USER
I use this image to recreate environments for R scripting purposes. This was working for a number of months up until the end of September when I started getting:
Error in curl::curl_fetch_memory(url, handle = handle) :
SSL certificate problem: certificate has expired
This occured when using GET request to query a website. How do I update my certificate now and in the future to avoid certificates expiring...I do not want to use the "config(ssl_verifypeer = FALSE)" workaround.
This happened to me too. Any chance you are working on MacOS or Linux based machine? It seems to be a bug:
https://security.stackexchange.com/questions/232445/https-connection-to-specific-sites-fail-with-curl-on-macos
ca-certificates Mac OS X
SSL Certificates - OS X Mavericks
Instead of adjusting the certificates as is suggested here, you can simply set R to not verify the peer. Just add the following line to the beginning of your code.
httr::set_config(config(ssl_verifypeer = FALSE, ssl_verifyhost = FALSE))

Not found bin/sh Docker Image

I am trying to deploy my Shiny app on my own server, but I cannot solve this issue. This is my docker image
FROM rocker/tidyverse
## App folder
WORKDIR /home/admin/shinyAppCovid/
ADD /shinyApp /shinyApp/
ADD /dependencias.txt /usr/local/bin/
RUN apt-get update && apt-get install -y \
sudo \
pandoc \
pandoc-citeproc \
libcurl4-gnutls-dev \
libcairo2-dev \
libxt-dev \
libssl-dev \
libssh2-1-dev
# expose port
EXPOSE 8080
# Download and install library
RUN R -e 'install.packages(read.csv("/usr/local/bin/dependencias.txt")[,1], depencencies=TRUE, repos="http://cran.rstudio.com/")'
# run app on container start
CMD ['/usr/bin/R', "-e", "shiny::runApp("/shinyApp/shiny", host = '0.0.0.0', port = 8080)"]
The problem is that when i a try to run a container this is what i have
/bin/sh: 1: [/usr/bin/R,: not found
I changed the last line for jus R and is not working I am still having the same issue, How can I do?

cannot install devtools in r shiny docker

I'm trying to build a docker image for my shiny app. Below is my dockerfile. When I build my images, everything else seems fine, except I got error message Error in library(devtools) : there is no package called ‘devtools’ Execution halted. I also tried devtools::install_github('nik01010/dashboardthemes') with no success. I have non clue why? What could go wrong? Do anyone know what is wrong with my dockerfile? Thanks a lot.
# Install R version 3.6
FROM r-base:3.6.0
# Install Ubuntu packages
RUN apt-get update && apt-get install -y \
sudo \
gdebi-core \
pandoc \
pandoc-citeproc \
libcurl4-gnutls-dev \
libcairo2-dev/unstable \
libxt-dev \
libssl-dev
# Download and install ShinyServer (latest version)
RUN wget --no-verbose https://s3.amazonaws.com/rstudio-shiny-server-os-build/ubuntu-12.04/x86_64/VERSION -O "version.txt" && \
VERSION=$(cat version.txt) && \
wget --no-verbose "https://s3.amazonaws.com/rstudio-shiny-server-os-build/ubuntu-12.04/x86_64/shiny-server-$VERSION-amd64.deb" -O ss-latest.deb && \
gdebi -n ss-latest.deb && \
rm -f version.txt ss-latest.deb
# Install R packages that are required
RUN R -e "install.packages(c('devtools', 'shiny','shinythemes','shinydashboard','shinyWidgets','shinyjs', 'tidyverse', 'dplyr', 'ggplot2','rlang','DT','lubridate', 'plotly', 'leaflet', 'mapview', 'tigris', 'rgdal', 'visNetwork', 'wordcloud2', 'arules'), repos='http://cran.rstudio.com/')"
RUN R -e "library(devtools)"
RUN R -e "install_github('nik01010/dashboardthemes')"
# 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"]
There are a few approaches you might try.
Easiest:
Use remotes::install_github instead of devtools. remotes has many fewer dependencies if you don't need the other functionality.
Second Easiest:
Use rocker/tidyverse image from Docker Hub instead of baseR image.
docker pull rocker/tidyverse
Change line 2:
FROM rocker/verse
Hardest:
Otherwise, you will need to figure out which dependencies you need to install inside your docker image before you can install devtools. It will likely be obvious if you try to install it interactively.
Make sure the container is running
Get the container name using docker ps
Start a shell with docker exec -it <container name> /bin/bash
Start R and try to install devtools interactively

Error connecting to sql server with docker, rbase, and RODBC

I'm trying to access a SQL Server on a local intranet with R, where R is loaded in a docker file with R-base.
On my desktop, I create a ODBC driver and it recognizes my windows credentials with the following:
odbcDriverConnect('driver={SQL Server};server=<serverName>;database=<dbName>;trusted_connection=yes')
Using R-base, after a lot of trial and error I was able to get RODBC installed, but I have no idea how to connect to this server.
Warning messages:
[RODBC] ERROR: state 01000, code 0, message [unixODBC][Driver Manager]Can't open lib 'SQL Server' : file not found
The DockerFile, which was built with a lot of trial and error. I'm sure much of it is redundant. Also I exposed ports which I found doing some research but I have no idea if they are needed to be exposed.
FROM openanalytics/r-base
# system libraries of general use
RUN apt-get update && apt-get install -y \
sudo \
libcairo2-dev \
libxt-dev \
libcurl4-gnutls-dev \
libssl-dev \
libssh2-1-dev \
libssl1.0.0
# RODBC
RUN apt-get update && \
apt-get install -y --no-install-recommends \
unixodbc \
unixodbc-dev \
unixodbc \
unixodbc-dev \
r-cran-rodbc \
apt-transport-https \
libssl-dev \
libsasl2-dev \
openssl \
curl \
unixodbc \
gnupg \
libc6 libc6-dev libc6-dbg
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN curl https://packages.microsoft.com/config/ubuntu/18.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
#RUN curl https://packages.microsoft.com/config/ubuntu/19.10/prod.list > /etc/apt/sources.list.d/mssql-release.list
RUN apt-get update -y
RUN ACCEPT_EULA=Y apt-get install -y msodbcsql17 unixodbc-dev mssql-tools
EXPOSE 3838
EXPOSE 8787
Once I build the file, I enter R studio and use install.packages("RODBC")
How can I connect to the SQL server from the docker file?
----------Update
after creating a docker container with Ubuntu, I found that I was not able to ping the server name. I was however, able to ping the server ip address.
I modified the search string to ping the ip address, and changed the driver, as follows:
odbcDriverConnect('driver={ODBC Driver 17 for SQL Server};server=<serverName>;database=<dbName>;trusted_connection=yes')
This still did not work.
I could not figure out how to connect to the database with the Windows credentials of the docker host inside of the docker virtual machine.
I had to make a username and password for the database and connect with that.
I think if you change your driver to FreeTDS this will work. R doesn't support Sql Server drivers unless its a pro plan.

Resources