Docker with Rstudio & conda virtual env - unable to load R packages

67 views Asked by At

I have this dockerfile:

# Use the rocker/rstudio image with R version 4.1.2
FROM rocker/rstudio:4.1.2

# Install deps
RUN apt-get update && apt-get install -y \
    wget \
    bzip2 \
    bash-completion \
    libxml2-dev \
    zlib1g-dev \
    libxtst6 \
    libxt6 \
    libhdf5-dev \
    libcurl4-openssl-dev \
    libssl-dev \
    libfontconfig1-dev \
    libcairo2-dev \
    libudunits2-dev \
    libgdal-dev \
    && rm -rf /var/lib/apt/lists/*


# Copy the package list file into the container
COPY r-requirements.txt /tmp/r-requirements.txt

# Install R packages from the list
RUN echo "Checkpoint installing R packages" && \
    while IFS= read -r line; do \
        case "${line}" in \
            *"==CRAN=="*) R -e "install.packages('${line%%==*}', repos='http://cran.rstudio.com/', dependencies=TRUE, version='${line##*==}')";; \
            *"==GITHUB=="*) R -e "devtools::install_github('${line%%==*}')";; \
            *"==BIOMGR=="*) R -e "BiocManager::install('${line%%==*}')";; \
        esac \
    done < /tmp/r-requirements.txt && \
echo "Checkpoint after installing R packages"

# Miniconda3 for Linux-x86_64
RUN wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O /tmp/miniconda.sh && \
    bash /tmp/miniconda.sh -b -p /opt/conda && \
    rm /tmp/miniconda.sh

# Add Miniconda3 to the PATH
ENV PATH="/opt/conda/bin:${PATH}"

# Create a virtual environment r-reticulate.yml
COPY r-reticulate.yml /tmp/r-reticulate.yml
RUN conda env create -f /tmp/r-reticulate.yml && \
    rm /tmp/r-reticulate.yml


# Expose RStudio port
EXPOSE 8787

# Set up entry point to run RStudio with conda environment activated
CMD ["bash", "-c", "source activate r-reticulate && /init"]

this r-requirements.txt file:

tidyverse==2.0.0==CRAN==
ggplot2==3.4.1==CRAN==
reticulate==1.28==CRAN==
keras==2.11.0==CRAN==
caret==6.0-93==CRAN==
BiocManager==3.13==CRAN==

#Github packages
grimbough/rhdf5filters==GITHUB==

#Bioconductor packages
rhdf5==BIOMGR==

And this r-reticulate.yml:

name: r-reticulate
channels:
  - conda-forge
  - defaults
dependencies:
  - _libgcc_mutex=0.1=conda_forge
  - _openmp_mutex=4.5=2_gnu
  - bzip2=1.0.8=h7f98852_4
  - ca-certificates=2022.12.7=ha878542_0
  - ld_impl_linux-64=2.39=hcc3a1bd_1
  - libffi=3.4.2=h7f98852_5
  - libgcc-ng=12.2.0=h65d4601_19
  - libgomp=12.2.0=h65d4601_19
  - libnsl=2.0.0=h7f98852_0
  - libsqlite=3.40.0=h753d276_0
  - libuuid=2.32.1=h7f98852_1000
  - libzlib=1.2.13=h166bdaf_4
  - ncurses=6.3=h27087fc_1
  - openssl=3.0.7=h0b41bf4_1
  - pip=22.3.1=pyhd8ed1ab_0
  - python=3.8.15=h4a9ceb5_0_cpython
  - readline=8.1.2=h0f457ee_0
  - setuptools=65.6.3=pyhd8ed1ab_0
  - tk=8.6.12=h27826a3_0
  - wheel=0.38.4=pyhd8ed1ab_0
  - xz=5.2.6=h166bdaf_0
  - pip:
    - h5py==3.7.0
    - keras==2.11.0
    - numpy==1.24.1
    - tensorflow==2.11.0

I want to build a docker container in which I would be able to run RStudio and launch R packages from within it. I also want to be able to use virtual env r-reticulate via reticulate package from within this RStudio instance. Once I build container, I can't load R packages via library(), RStudio throws an error informing that such package was not installed, however the installation had 0 exit status, so the library should be there.

I think that I probably mess around with shells, but I am unable to figure it out by myself. Would appreciate help, since I did not find a solution on the internet. I want R packages to be kept outside from the env, the env is for certain version of python & tensorflow only.

In ideal world I would like the virtual env to be accessible from RStudio, but depending on users input e.g. whether they pass reticulate::use_condaenv("r-reticulate") or not) - this is the option I could not figure out.

In less ideal option, I would like conda env be activated along RStudio on init - that is the option I tried to make, to no avail.

Would appreciate advice.

The.yml and .txt files IRL contain more specs, this is just a minimal reprex.

0

There are 0 answers