Skip to content

Commit

Permalink
Refactor Dockerfile for R environment setup and package installation
Browse files Browse the repository at this point in the history
- Updated base image to rocker/r-ver:4.4.2 for improved compatibility and performance.
- Enhanced package installation process with caching to optimize build times.
- Streamlined installation of CRAN and GitHub packages, including a fallback mechanism for the optiSel package.
- Consolidated runtime dependencies installation into a single layer to reduce image size and improve efficiency.
- Improved directory structure and permissions for better organization and access control.

These changes aim to enhance the efficiency and maintainability of the Docker setup for the R environment.
  • Loading branch information
jhagberg committed Jan 8, 2025
1 parent b7a4e13 commit 28ca038
Showing 1 changed file with 66 additions and 49 deletions.
115 changes: 66 additions & 49 deletions .docker/r-api
Original file line number Diff line number Diff line change
@@ -1,52 +1,69 @@
FROM rocker/r-ver:4.2 as R-build

RUN apt update \
&& apt install -y --no-install-recommends \
libpq-dev \
libglu1-mesa-dev \
libpng-dev \
libgl1-mesa-dev \
zlib1g-dev \
libudunits2-dev \
libgdal-dev \
gdal-bin \
libgeos-dev \
libproj-dev \
libsqlite3-dev \
pandoc\
make \
cmake \
libicu-dev \
libfreetype6-dev \
libmagick++-dev \
libcurl4-openssl-dev \
libssl-dev \
libxml2-dev \
pkg-config \
libsodium-dev \
libicu-dev \
&& apt autoremove -y \
&& rm -rf /var/lib/apt/lists/*

RUN install2.r -d TRUE --error --ncpus 6 --repos "https://packagemanager.rstudio.com/cran/__linux__/jammy/2023-02-24" --repos "https://ftp.acc.umu.se/mirror/CRAN/" --repos "https://cloud.r-project.org/" optiSel readr plumber rjson dplyr remotes
RUN installGithub.r rmsharp/nprcgenekeepr


FROM rocker/r-ver:4.2

COPY --from=R-build /usr/local/lib/R/site-library /usr/local/lib/R/site-library

RUN apt-get update \
&& apt-get install -y --no-install-recommends \
libpq5 \
libglu1-mesa \
pandoc \
supervisor \
rsync \
inotify-tools && \
mkdir -p /code /usr/local/r-api && \
chown www-data:www-data /usr/local/r-api && \
rm -rf /var/lib/apt/lists/*
FROM rocker/r-ver:4.4.2 as r-build

# Install build dependencies
RUN --mount=type=cache,target=/var/cache/apt \
--mount=type=cache,target=/var/lib/apt \
apt-get update && apt-get install -y --no-install-recommends \
libpq-dev \
libglu1-mesa-dev \
libpng-dev \
libgl1-mesa-dev \
zlib1g-dev \
libudunits2-dev \
libgdal-dev \
gdal-bin \
libgeos-dev \
libproj-dev \
libsqlite3-dev \
pandoc \
make \
cmake \
libicu-dev \
libfreetype6-dev \
libmagick++-dev \
libcurl4-openssl-dev \
libssl-dev \
libxml2-dev \
pkg-config \
libsodium-dev \
&& rm -rf /var/lib/apt/lists/*

# Create directory for package caching
RUN mkdir -p /pkg_cache

# Install CRAN packages more efficiently with caching (excluding optiSel)
RUN --mount=type=cache,target=/pkg_cache \
PKGS="readr plumber rjson dplyr remotes" && \
install2.r --error --ncpus $(nproc) --skipinstalled --repos https://packagemanager.posit.co/cran/__linux__/noble/2025-01-08 $PKGS && \
rm -rf /tmp/downloaded_packages

# Try to install optiSel binary first, fall back to source if needed
RUN --mount=type=cache,target=/pkg_cache \
Rscript -e 'options(repos = c(CRAN = "https://packagemanager.posit.co/cran/__linux__/noble/2025-01-08")); install.packages("optiSel", dependencies=TRUE)'

# Install GitHub package with caching
RUN --mount=type=cache,target=/pkg_cache \
Rscript -e 'options(repos = c(CRAN = "https://packagemanager.posit.co/cran/__linux__/noble/2025-01-08")); remotes::install_github("rmsharp/nprcgenekeepr", upgrade = "never", dependencies = TRUE)' && \
rm -rf /tmp/downloaded_packages

FROM rocker/r-ver:4.4.2

# Copy R libraries from build stage
COPY --from=r-build /usr/local/lib/R/site-library /usr/local/lib/R/site-library

# Install runtime dependencies and set up directories in a single layer
RUN --mount=type=cache,target=/var/cache/apt \
--mount=type=cache,target=/var/lib/apt \
apt-get update && apt-get install -y --no-install-recommends \
libpq5 \
libglu1-mesa \
pandoc \
supervisor \
rsync \
inotify-tools \
&& rm -rf /var/lib/apt/lists/* \
&& mkdir -p /code /usr/local/r-api \
&& chown www-data:www-data /usr/local/r-api

WORKDIR /usr/local/r-api

Expand Down

0 comments on commit 28ca038

Please sign in to comment.