Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot Install R Packages in Docker Image

Tags:

java

docker

r

It seems because of legal reasons Java is not able to be installed separately in a docker image but rather we have to use a java image.

I am using a java image, with R installed as I need it, however when I am installing an R package I get an error

Here is my Dockerfile below:

FROM buildpack-deps:jessie-scm

# A few problems with compiling Java from source:
#  1. Oracle.  Licensing prevents us from redistributing the official JDK.
#  2. Compiling OpenJDK also requires the JDK to be installed, and it gets
#       really hairy.

RUN apt-get update && apt-get install -y --no-install-recommends \
        bzip2 \
        unzip \
        xz-utils \
    && rm -rf /var/lib/apt/lists/*

# Default to UTF-8 file.encoding
ENV LANG C.UTF-8

# add a simple script that can auto-detect the appropriate JAVA_HOME value
# based on whether the JDK or only the JRE is installed
RUN { \
        echo '#!/bin/sh'; \
        echo 'set -e'; \
        echo; \
        echo 'dirname "$(dirname "$(readlink -f "$(which javac || which java)")")"'; \
    } > /usr/local/bin/docker-java-home \
    && chmod +x /usr/local/bin/docker-java-home

ENV JAVA_HOME /usr/lib/jvm/java-7-openjdk-amd64

ENV JAVA_VERSION 7u111
ENV JAVA_DEBIAN_VERSION 7u111-2.6.7-2~deb8u1

RUN set -x \
    && apt-get update \
    && apt-get install -y \
        openjdk-7-jdk="$JAVA_DEBIAN_VERSION" \
    && rm -rf /var/lib/apt/lists/* \
    && [ "$JAVA_HOME" = "$(docker-java-home)" ]

# If you're reading this and have any feedback on how this image could be
#   improved, please open an issue or a pull request so we can discuss it!

# system libraries of general use
RUN apt-get update && apt-get install -y \
    sudo \
    pandoc \
    pandoc-citeproc \
    libcurl4-gnutls-dev \
    libcairo2-dev \
    libxt-dev \
    libssl-dev \
    libssh2-1-dev \
    libssl1.0.0

# system library dependency for the euler app
RUN apt-get update && apt-get install -y \
    libmpfr-dev

RUN sudo apt-get install -y \
    r-base r-base-dev

# basic shiny functionality
RUN R -e "install.packages(c('shiny', 'rmarkdown'), repos='https://cloud.r-project.org/')"

# install dependencies
RUN R -e "install.packages('Rmpfr', repos='https://cloud.r-project.org/')"

# Special Package
RUN R -e "install.packages('shiny')"
RUN R -e "install.packages('shinydashboard')"
RUN R -e "install.packages('plyr')"
RUN R -e "install.packages('dplyr')"
RUN R -e "install.packages('ggplot2')"
RUN R -e "install.packages('tm')"
RUN R -e "install.packages('SnowballC')"
RUN R -e "install.packages('wordcloud')"
RUN R -e "install.packages('RWeka')"
RUN R -e "install.packages('reshape2')"
RUN R -e "install.packages('igraph')"



# copy the app to the image
RUN mkdir /root/testapp1
COPY testapp1 /root/testapp1

COPY Rprofile.site /usr/lib/R/etc/

EXPOSE 3838

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

When I try to install any R package I get this error below:

> install.packages('shiny')
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
Error in contrib.url(repos, type) : 
  trying to use CRAN without setting a mirror
Calls: install.packages -> grep -> contrib.url
Execution halted
The command '/bin/sh -c R -e "install.packages('shiny')"' returned a non-zero code: 1

How can I resolve this headache of an issue.

Thanks.

like image 427
Techno04335 Avatar asked Jan 11 '17 22:01

Techno04335


People also ask

Why is my R not installing packages?

Changing the configuration in R Studio to solve install packages issue. Go To Tools -> Global option -> Packages. Then uncheck the option “Use secure download method for HTTP”. For other RStudio issues refer to official Troubleshooting Guide here.

Can I install packages in Docker container?

To install packages in a docker container, the packages should be defined in the Dockerfile. If you want to install packages in the Container, use the RUN statement followed by exact download command . You can update the Dockerfile with latest list of packages at anytime and build again to create new image out of it.

Does Docker work with R?

Docker is designed to enclose environments inside an image / a container. What this allows, for example, is to have a Linux machine on a Macbook, or a machine with R 3.3 when your main computer has R 3.5.


2 Answers

When install.packages('shiny') is executed in R environment it hang asking you to select from what mirror you want to download.

> install.packages('shiny')

Installing package into ‘/Users/user/Library/R/3.3/library’
(as ‘lib’ is unspecified)

--- Please select a CRAN mirror for use in this session ---
HTTPS CRAN mirror

 1: 0-Cloud [https]                 
 2: Algeria [https]
 ... 
 55: (HTTP mirrors)

Selection: 

According to your error output seems that the command RUN R -e "install.packages(c('shiny', 'rmarkdown'), repos='https://cloud.r-project.org/')"

and the next one are executed, and then fails (error exit 1) when try to execute:

RUN R -e "install.packages('shiny')"

Providing instead install.packages('shiny', repos='https://cloud.r-project.org/') will probably do a silent install without asking anything.

like image 169
exoddus Avatar answered Sep 20 '22 07:09

exoddus


Looks like the errors are you are getting are from the install.pacakges methods which don't have the repos argument specified.

Once I updated the install.packages with repos='http://cran.us.r-project.org/' in my local machine with your dockerfile, the image was building successfully.

like image 38
Socratees Samipillai Avatar answered Sep 23 '22 07:09

Socratees Samipillai