Unable to install RPostgres package in Docker container

I am new to the concept of containerising R shiny apps using Docker and I was trying to deploy my R shiny application on Azure cloud environment by deploying a docker container image. This R shiny app tries to connect to a PostgreSQL DB which has already been setup on Azure cloud environment. So in order to connect R shiny app to cloud based PostgreSQL DB, I am trying to install RPostgres,RPostgreSQL and DBI packages for establishing the connection, in my docker image.

But I am getting the following error: I am getting the following error - Error in library(RPostgres) : there is no package called ‘RPostgres’ Calls: ... sourceUTF8 -> eval -> eval -> ..stacktraceon.. -> library Execution halted

Attached below is the Dockerfile that I am using:

# Use an R runtime as a base image
FROM rocker/shiny:latest

# Make a directory in the container
RUN mkdir /home/shiny-app

# Copy the Shiny app files into the container
COPY app.R /home/shiny-app/app.R

# Install R dependencies
RUN R -e "install.packages(c('shiny','DT', 'data.table', 'shinythemes','dplyr','readxl','sqldf','lubridate','shinyjs','shinymanager','shinyalert','shinyFeedback','RPostgres','RPostgreSQL','DBI'))"

# Expose the application port
EXPOSE 8000

# Command to run the Shiny app
CMD ["R", "-e", "shiny::runApp('/home/shiny-app/app.R')"]

I also tried to install the packages by using below method but I am still having the same issue:

RUN R -e "install.packages('RPostgres',dependencies=TRUE, repos='http://cran.rstudio.com/')"
RUN R -e "install.packages('RPostgreSQL',dependencies=TRUE, repos='http://cran.rstudio.com/')"
RUN R -e "install.packages('DBI',dependencies=TRUE, repos='http://cran.rstudio.com/')"

NOTE 1: I already tried to deploy the R shiny app without the R packages RPostgres, RPostgreSQL and DBI and I was successful in deploying the shiny app in Docker image.

NOTE 2: When I run the R shiny app on local machine and try to connect to PostgreSQL DB on Azure, I am able to establish the connection and perform add, update and delete on the PostgreSQL DB.

Can someone please help me to resolve this issue or suggestions to modify my dockerfile for the same ?

You are missing system requirements - the install.packages() command you run produces an error when trying to compile the RPostgreSQL package (and similarly so the RPostgres package).

This can be fixed by adding

# Install system requirement
RUN apt-get update && apt-get install libpq-dev 

just before the install.packages section of your Dockerfile.

Alternatively I can recommend to replace install.packages by pak::pak - pak uses a new approach for package installation that - when run as admin/root even figures out all the system requirements and installs them as needed.

So, the install.packages() line would change to

# Install R dependencies using https://pak.r-lib.org
RUN R -e "install.packages('pak')"
RUN R -e "pak::pak(c('shiny','DT', 'data.table', 'shinythemes','dplyr','readxl','sqldf','lubridate','shinyjs','shinymanager','shinyalert','shinyFeedback','RPostgres','RPostgreSQL','DBI'))"

Using pak you will not only get system dependencies "for free", the package installation will also be much faster as it will both download and install packages in parallel.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.