Creating Docker with Ruby and R - r

I have a rails application that combines some R code. It works fine, and now is the time to dockerize it. I tried creating the docker based on a ruby image, which failed installing R properly, and then the other way around (R image, installing ruby using rbenv as explained here) which failed too.
Has anyone experienced with this combo?
EDIT: I have managed to create a docker using the R image, however this requires a tiring installation of many ruby dependencies. So consider this side done.
Still, why won't the other way (installing R on ruby image) work?
Ruby-based:
FROM ruby:2.7.1 AS rails-toolbox
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
RUN mkdir /app
WORKDIR /app
ADD shalev/Gemfile /app/Gemfile
ADD shalev/Gemfile.lock /app/Gemfile.lock
RUN bundle install --without development test doc --jobs=4
RUN apt-get install -y apt-utils
RUN apt-get install -y apt-transport-https
RUN wget -qO- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | tee -a /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc
RUN echo "deb https://cloud.r-project.org/bin/linux/ubuntu bionic-cran35/" | tee -a /etc/apt/sources.list
RUN apt-get update
RUN apt-get install r-base
COPY shalev/ /app/
[...]
error message:
Step 13/20 : RUN apt-get install r-base
---> Running in f546cfe57d33
Reading package lists...
Building dependency tree...
Reading state information...
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:
The following packages have unmet dependencies:
r-base : Depends: r-base-core (>= 3.6.3-1bionic) but it is not going to be installed
Depends: r-recommended (= 3.6.3-1bionic) but it is not going to be installed
Recommends: r-base-html but it is not going to be installed
E: Unable to correct problems, you have held broken packages.
The command '/bin/sh -c apt-get install r-base' returned a non-zero code: 100
R-based:
FROM rocker/r-ver:3.6.3
RUN apt-get update -qq && apt-get install -y apt-utils apt-transport-https build-essential libpq-dev nodejs
RUN apt-get install -y git-core zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev nodejs yarn curl bzip2
RUN git clone https://github.com/rbenv/rbenv.git ~/.rbenv \
&& echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc \
&& echo 'eval "$(rbenv init -)"' >> ~/.bashrc
ENV HOME /home/shalev
ENV PATH "$HOME/.rbenv/bin:$HOME/.rbenv/shims:$PATH"
ENV RUBY_VERSION 2.7.1
RUN mkdir -p "$(rbenv root)"/plugins \
&& git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build
RUN rbenv install $RUBY_VERSION
RUN rbenv global $RUBY_VERSION && rbenv versions && ruby -v
RUN mkdir /app
[...]
error message:
Step 9/25 : RUN rbenv install $RUBY_VERSION
---> Running in 9939299d6ed1
/bin/sh: 1: rbenv: not found

You can use the stand alone version of ruby-build.
Note that you are root and not shalev inside the container:
FROM rocker/r-ver:3.6.3
RUN apt-get update -qq && \
apt-get install -y \
apt-utils apt-transport-https \
build-essential libpq-dev nodejs
RUN apt-get install -y \
git-core zlib1g-dev build-essential \
libssl-dev libreadline-dev libyaml-dev \
libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev \
libcurl4-openssl-dev software-properties-common \
libffi-dev nodejs yarn curl bzip2
RUN git clone https://github.com/rbenv/rbenv.git ~/.rbenv \
&& echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc \
&& echo 'eval "$(rbenv init -)"' >> ~/.bashrc
ENV PATH "/root/.rbenv/bin/:/root/.rbenv/shims/:$PATH"
ENV RUBY_VERSION 2.7.1
RUN git clone https://github.com/rbenv/ruby-build.git && \
PREFIX=/usr/local ./ruby-build/install.sh
RUN rbenv install $RUBY_VERSION && \
rbenv global $RUBY_VERSION && \
rbenv versions
RUN mkdir /app

I managed to make it work with the ruby image as well:
FROM ruby:2.7.1 AS rails-toolbox
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
RUN apt-get install -y apt-utils apt-transport-https
RUN apt-get install -y gfortran
RUN wget -c https://cran.r-project.org/src/base/R-3/R-3.6.3.tar.gz
RUN tar -xf R-3.6.3.tar.gz
WORKDIR "/R-3.6.3"
RUN ./configure
RUN make -j9
RUN make install
WORKDIR "/root"
RUN mkdir /app
[...]

Related

`google-chrome` and `chromium-browser` were not found. Try setting the CHROMOTE_CHROME environment variable or adding one of

I have a docker image with RStudio installed.
My flexdashboard app has a section to download a flextable
save_path = str_c(image_dir, "IX_overviewp2_", sub('-','_',current_quarter), ".png")
save_as_image(ft,
path = save_path,
zoom = 3 ,
webshot = "webshot2") # save flextable as png
but when I get to the section of the code, I receive the error: "google-chrome" and "chromium-browser" were not found. Try setting the CHROMOTE_CHROME environment variable or adding one of these executables to your PATH. Error in initialize(...) : Invalid path to Chrome
I updated my docker image to include chrome with the last two RUN commands of my dockerfile, but this still does not solve my issue.
Does anyone have any suggestions?
FROM rocker/verse
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y --no-install-recommends build-essential libpq-dev python3.8 python3-pip python3-setuptools python3-dev
RUN pip3 install --upgrade pip
ADD . ./home/rstudio
ADD requirements.txt .
ADD install_packages.r .
# Miniconda and dependencies
RUN cd /tmp/ && \
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh && \
bash Miniconda3-latest-Linux-x86_64.sh -b -p $HOME/miniconda3 && \
/root/miniconda3/condabin/conda install -y python=3.7
ENV PATH=$PATH:/root/miniconda3/bin
#RUN npm install phantomjs-prebuilt --phantomjs_cdnurl=http://cnpmjs.org/downloads
# installing python libraries
RUN pip3 install -r requirements.txt
# installing r libraries
RUN Rscript install_packages.r
RUN if ! [[ "16.04 18.04 20.04 21.04 21.10" == *"$(lsb_release -rs)"* ]]; \
then \
echo "Ubuntu $(lsb_release -rs) is not currently supported."; \
exit; \
fi
RUN sudo su
RUN apt-get update && apt-get install -y gnupg2
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN curl https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/prod.list > /etc/apt/sources.list.d/mssql-release.list
RUN exit
RUN sudo apt-get update
RUN sudo ACCEPT_EULA=Y apt-get install -y msodbcsql17
RUN chmod -R 777 /home/rstudio
# Install Chrome WebDriver
RUN CHROMEDRIVER_VERSION=`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE` && \
mkdir -p /opt/chromedriver-$CHROMEDRIVER_VERSION && \
curl -sS -o /tmp/chromedriver_linux64.zip http://chromedriver.storage.googleapis.com/$CHROMEDRIVER_VERSION/chromedriver_linux64.zip && \
unzip -qq /tmp/chromedriver_linux64.zip -d /opt/chromedriver-$CHROMEDRIVER_VERSION && \
rm /tmp/chromedriver_linux64.zip && \
chmod +x /opt/chromedriver-$CHROMEDRIVER_VERSION/chromedriver && \
ln -fs /opt/chromedriver-$CHROMEDRIVER_VERSION/chromedriver /usr/local/bin/chromedriver
# Install Google Chrome
RUN curl -sS -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - && \
echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list && \
apt-get -yqq update && \
apt-get -yqq install google-chrome-stable && \
rm -rf /var/lib/apt/lists/*
Try adding in you dockerfile:
RUN wget https://downloads.vivaldi.com/stable/vivaldi-stable_5.5.2805.35-1_amd64.deb
RUN apt-get update && apt-get install -y ./vivaldi-stable_5.5.2805.35-1_amd64.deb && rm -rf /var/lib/apt/lists/*
RUN echo CHROMOTE_CHROME=/usr/bin/vivaldi >> .Renviron

Docker image has error when running from R Studio

My Dockerfile contains the exact code as shown in the statworx website to build an image for a Shiny app. Whenever I run the following code however:
# Base image https://hub.docker.com/u/rocker/
FROM rocker/shiny:latest
# system libraries of general use
## install debian packages
RUN apt-get update -qq && apt-get -y --no-install-recommends install
libxml2-dev
libcairo2-dev
libsqlite3-dev
libmariadbd-dev
libpq-dev
libssh2-1-dev
unixodbc-dev
libcurl4-openssl-dev
libssl-dev
## update system libraries
RUN apt-get update &&
apt-get upgrade -y &&
apt-get clean
# copy necessary files
## app folder
COPY /example-app ./app
## renv.lock file
COPY /example-app/renv.lock ./renv.lock
# install renv & restore packages
RUN Rscript -e 'install.packages("renv")'
RUN Rscript -e 'renv::consent(provided = TRUE)'
RUN Rscript -e 'renv::restore()'
# expose port
EXPOSE 3838
# run app on container start
CMD ["R", "-e", "shiny::runApp('/app', host = '0.0.0.0', port = 3838)"]
docker build -t my-shinyapp-image .
I get the following error: failed to solve with frontend dockerfile.v0: failed to create LLB definition: dockerfile parse error line 7: unknown instruction: LIBXML2-DEV
If I remove the LIBXML2-DEV package it gives an error on the next one. Has anyone encountered this problem before?
Following the suggestion of #HubertL, here is the Dockerfile that will work:
# Base image https://hub.docker.com/u/rocker/
FROM rocker/shiny:latest
# system libraries of general use
## install Debian/Ubuntu packages
RUN apt-get update -qq && \
apt-get upgrade -y && \
apt-get -y --no-install-recommends install \
libxml2-dev \
libcairo2-dev \
libsqlite3-dev \
libmariadbd-dev \
libpq-dev \
libssh2-1-dev \
unixodbc-dev \
libcurl4-openssl-dev \
libssl-dev
## update system libraries
RUN apt-get update && \
apt-get upgrade -y && \
apt-get clean
# copy necessary files
## app folder
COPY /example-app ./app
## renv.lock file
COPY /example-app/renv.lock ./renv.lock
# install renv & restore packages
RUN Rscript -e 'install.packages("renv")'
RUN Rscript -e 'renv::consent(provided = TRUE)'
RUN Rscript -e 'renv::restore()'
# expose port
EXPOSE 3838
# run app on container start
CMD ["R", "-e", "shiny::runApp('/app', host = '0.0.0.0', port = 3838)"]

/usr/local/lib64/R/lib/libR.so: cannot open shared object file: No such file or directory

I am getting this error while making an image through docker on lambda.
** [ERROR] OSError: cannot load library '/usr/local/lib64/R/lib/libR.so': /usr/local/lib64/R/lib/libR.so: cannot open shared object file: No such file or directory**
Docker file:
FROM public.ecr.aws/lts/ubuntu:latest
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y wget
RUN apt-get install -y software-properties-common
RUN apt-get install -y gcc gfortran build-essential
RUN apt-get update
RUN apt-get install -y libcurl4-openssl-dev
RUN apt-get install -y zlib1g zlib1g-dev libbz2-dev liblzma-dev libpcre++-dev libpango1.0-dev \
xorg-dev \
libreadline-dev
RUN wget -c https://cran.r-project.org/src/base/R-3/R-3.5.0.tar.gz && tar -xf R-3.5.0.tar.gz
RUN cd R-3.5.0 && ./configure --with-readline=no --with-x=no && make && make install
RUN R -e "install.packages('remotes', repos = 'http://cran.us.r-project.org')"
RUN R -e "require(remotes)"
RUN R -e "remotes::install_version('text2vec', version = '0.5.1', repos = 'http://cran.us.r-project.org')"
RUN export LD_LIBRARY_PATH="/usr/local/lib64:$LD_LIBRARY_PATH"
COPY requirements.txt ./
RUN apt-get update && apt-get install -y \
python3.8 \
python3-pip
RUN pip3 install -r requirements.txt
RUN pip3 install \
awslambdaric
RUN ln -s /usr/lib64/R/lib/* /usr/local/lib64/R/lib/
COPY lambda_function.py ./
ENTRYPOINT [ "/usr/bin/python3", "-m", "awslambdaric" ]
CMD ["lambda_function.lambda_handler"]
Any help would be appreciated!
I cleaned up the first parts of your Dockerfile as
FROM public.ecr.aws/lts/ubuntu:latest
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y \
wget \
software-properties-common \
gcc gfortran build-essential \
libcurl4-openssl-dev \
zlib1g zlib1g-dev libbz2-dev liblzma-dev libpcre++-dev libpango1.0-dev \
xorg-dev \
libreadline-dev && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
RUN wget -c https://cran.r-project.org/src/base/R-3/R-3.5.0.tar.gz && \
tar -xf R-3.5.0.tar.gz && \
cd R-3.5.0 && \
./configure --with-readline=no --with-x=no --enable-R-shlib && \
make -j && make install
Putting all the apt-get into one RUN allows one to clean up by removing apt cache files. Using make -j builds R in parallel, which can speed up the build significantly.
I then built it, and launched the resulting image using bash to see that libR.so did indeed exist.
$ docker build .
...
Successfully built f1d83e97e8ef
$ docker run -it --rm f1d83e97e8ef bash
root#add29666d5d5:/# ls -l /usr/local/lib64/R/lib
total 21984
-rwxr-xr-x 1 root root 16815248 Sep 5 14:43 libR.so
-rwxr-xr-x 1 root root 462896 Sep 5 14:43 libRblas.so
-rwxr-xr-x 1 root root 5226296 Sep 5 14:43 libRlapack.so
libR.so exists and permissions (read and execute for all) are appropriate.
I don't think exporting LD_LIBRARY_PATH or creating a symbolic link should be necessary.

shiny docker image build failed due to ubuntu package issue

I'm trying to build docker image for my R shiny app. Below is theDockerfile.
# 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 \
libxt-dev \
xtail \
wget \
libudunits2-dev \
libgdal-dev
# Install R packages that are required
RUN R -e "install.packages(c('remotes', 'tidyr', 'dplyr', 'ggplot2', 'stringr','shiny', 'shinydashboard','shinyWidgets','shinyjs', 'rlang','scales','DT','lubridate', 'plotly', 'leaflet', 'leafpop', 'visNetwork', 'wordcloud2', 'arules'), repos='http://cran.rstudio.com/')"
RUN R -e "remotes::install_github('nik01010/dashboardthemes')"
# Download and install ShinyServer (latest version)
RUN wget --no-verbose https://download3.rstudio.org/ubuntu-14.04/x86_64/VERSION -O "version.txt" && \
VERSION=$(cat version.txt) && \
wget --no-verbose "https://download3.rstudio.org/ubuntu-14.04/x86_64/shiny-server-$VERSION-amd64.deb" -O ss-latest.deb && \
gdebi -n ss-latest.deb && \
rm -f version.txt ss-latest.deb
# 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"]
However, my image building failed quite early. Below is all the logs. It seems like some Ubuntu package issue which I do not quite understand.
I'm new to docker. I have not clue what to do with those error messages. Appreciate if anyone can help find out what needs to be done. Thanks.
Sending build context to Docker daemon 113.4MB
Step 1/10 : FROM r-base:3.6.0
---> 876f4d7b60e9
Step 2/10 : RUN apt-get update && apt-get install -y sudo gdebi-core pandoc pandoc-citeproc libcurl4-gnutls-dev libcairo2-dev libxt-dev xtail wget libudunits2-dev libgdal-dev
---> Running in 825a69c5a05f
Get:2 http://deb.debian.org/debian testing InRelease [116 kB]
Get:1 http://cdn-fastly.deb.debian.org/debian sid InRelease [146 kB]
Get:3 http://deb.debian.org/debian testing/main amd64 Packages [7,690 kB]
Get:4 http://cdn-fastly.deb.debian.org/debian sid/main amd64 Packages [8,225 kB]
Fetched 16.2 MB in 6s (2,724 kB/s)
Reading package lists...
Reading package lists...
Building dependency tree...
Reading state information...
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:
The following packages have unmet dependencies:
libc6-dev : Breaks: libgcc-8-dev (< 8.4.0-2~) but 8.3.0-7 is to be installed
E: Error, pkgProblemResolver::Resolve generated breaks, this may be caused by held packages.
The command '/bin/sh -c apt-get update && apt-get install -y sudo gdebi-core pandoc pandoc-citeproc libcurl4-gnutls-dev libcairo2-dev libxt-dev xtail wget libudunits2-dev libgdal-dev' returned a non-zero code: 100
The error message you are receiving is not Docker specific, but rather concerns your package manager apt.
Although you are updating the package list using apt-get update, you do not install newer versions of packages. What happens now is that you are trying to install a newer version of an already installed package which leads to the error message.
If you use apt-get upgrade, apt will install the newer versions of your installed packages.
Changing the 5th line as shown below will fix that issue:
RUN apt-get update && apt-get upgrade -y && apt-get install -y \

How to build Dockerfile with R & Java

I'm trying to build a Docker container that runs R with the package RJava. I have tried the following code:
# Install R version 3.6.3
FROM rocker/tidyverse:3.6.3
# Make ~/.R
RUN mkdir -p $HOME/.R
# Install Ubuntu packages && then R packages
RUN install2.r --error \
lubridate magrittr RPostgres DBI broom rlang rJava
However I get the following: installation of package ‘rJava’ had non-zero exit status.
Can anyone help me with this. I'm thinking that maybe it is because Java is not installed. Does anyone know how to install Java on this docker container?
I've tried adding the following to my dockerfile as per another post I found however I get the error saying 'The repository 'http://ppa.launchpad.net/webupd8team/java/ubuntu focal Release' does not have a Release file:
# Install "software-properties-common" (for the "add-apt-repository")
RUN apt-get update && apt-get install -y \
software-properties-common
# Add the "JAVA" ppa
RUN add-apt-repository -y \
ppa:webupd8team/java
# Install OpenJDK-8
RUN apt-get update && \
apt-get install -y openjdk-8-jdk && \
apt-get install -y ant && \
apt-get clean;
# Fix certificate issues
RUN apt-get update && \
apt-get install ca-certificates-java && \
apt-get clean && \
update-ca-certificates -f;
# Setup JAVA_HOME -- useful for docker commandline
ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64/
RUN export JAVA_HOME
I'm new to docker and any help with this would be much appreciated.
The rocker images are based on debian, not ubuntu. Specifically it is Debian GNU/Linux 10 (buster). With that version, you can install java by installing the package openjdk-11-jdk via apt and you don't need to add any repositories for openjdk-8-jdk.
So a working dockerfile that installs rJava:
FROM rocker/tidyverse:3.6.3
RUN apt-get update && \
apt-get install -y openjdk-11-jdk && \
apt-get install -y liblzma-dev && \
apt-get install -y libbz2-dev
RUN Rscript -e "install.packages('rJava')"
Note: liblzma-dev and libbz2-dev are additional system dependencies for compiling rJava.

Resources