diff --git a/.gitignore b/.gitignore index 5b6a065..45dfcdf 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ .Rhistory .RData .Ruserdata +*.html diff --git a/README.Rmd b/README.Rmd index 4c5bdcc..bb84e19 100644 --- a/README.Rmd +++ b/README.Rmd @@ -120,7 +120,44 @@ Add :tagname after ghcr.io/geocompx/imagename to get the image you want. docker run -e PASSWORD=pw --rm -p 8786:8787 ghcr.io/geocompx/buildbook ``` -## Examples +### Building leaner custom images with rocker-versioned2 + +The images above are opinionated "batteries included" builds. For users who want smaller images or a different package selection, the [rocker-versioned2](https://github.com/rocker-org/rocker-versioned2) repository provides modular `install_*.sh` scripts that can be composed on top of a slim base image. + +For example, to build a minimal R + geospatial image yourself: + +```Dockerfile +FROM ghcr.io/rocker-org/geospatial:4.4.2 +RUN /rocker_scripts/install_rstudio.sh +``` + +Or to add only what you need: + +```Dockerfile +FROM ghcr.io/rocker/r-ver:4.4.2 +RUN apt-get update && apt-get install -y --no-install-recommends \ + libgdal-dev libgeos-dev libproj-dev \ + && rm -rf /var/lib/apt/lists/* +RUN install2.r sf terra spData +``` + +Key [`rocker-versioned2` scripts](https://github.com/rocker-org/rocker-versioned2/tree/master/scripts) to mix and match: + +- [`/rocker_scripts/install_geospatial.sh`](https://github.com/rocker-org/rocker-versioned2/blob/master/scripts/install_geospatial.sh) — GDAL, PROJ, GEOS, and the core R geospatial packages (sf, terra, raster, stars, sp, etc.) +- [`/rocker_scripts/install_rstudio.sh`](https://github.com/rocker-org/rocker-versioned2/blob/master/scripts/install_rstudio.sh) — RStudio Server (uses S6 supervisor, ~300MB) +- [`/rocker_scripts/install_python.sh`](https://github.com/rocker-org/rocker-versioned2/blob/master/scripts/install_python.sh) — Python 3 + reticulate + venv +- [`/rocker_scripts/install_tidyverse.sh`](https://github.com/rocker-org/rocker-versioned2/blob/master/scripts/install_tidyverse.sh) — The tidyverse stack +- [`/rocker_scripts/install_quarto.sh`](https://github.com/rocker-org/rocker-versioned2/blob/master/scripts/install_quarto.sh) — Quarto CLI + +#### Suggested improvements upstream (for the Rocker project) + +1. **Provide a "geospatial-minimal" variant** in `rocker-versioned2` that omits RStudio Server and the heavy R geospatial package set, giving just the system libraries (GDAL, PROJ, GEOS, etc.) on top of `r-ver`. Users could then `install2.r sf terra` on top of a ~1–2GB base. +2. **Use a smaller base image** (e.g., `debian:bookworm-slim` or `ubuntu:22.04`) rather than the full `rocker/verse` for images that don't need TeX Live, pandoc, or Jupyter. +3. **Prune base image layers** — the [`books/rocker` best practices](https://rocker-project.org/use/extending.html) already recommend `--no-install-recommends` and `rm -rf /var/lib/apt/lists/*`, but the base images themselves could be rebuilt with multi-stage builds to discard build tools after package compilation. +4. **Make stripping of RSPM binary libraries opt-out** rather than opt-in (see [rocker-versioned2#340](https://github.com/rocker-org/rocker-versioned2/issues/340)) — this can save 20–30% on R package image size with no observed runtime cost. +5. **Offer a "pruned" `rocker/geospatial` tag** that has had LaTeX, pandoc, and Java removed (the largest contributors to image bloat) for users who only need the geospatial C libraries + R bindings. + +## Building the images locally ### osgeo diff --git a/README.md b/README.md index 51ce715..f69ced8 100644 --- a/README.md +++ b/README.md @@ -151,7 +151,73 @@ Add :tagname after ghcr.io/geocompx/imagename to get the image you want. docker run -e PASSWORD=pw --rm -p 8786:8787 ghcr.io/geocompx/buildbook ``` -## Examples +### Building leaner custom images with rocker-versioned2 + +The images above are opinionated “batteries included” builds. For users +who want smaller images or a different package selection, the +[rocker-versioned2](https://github.com/rocker-org/rocker-versioned2) +repository provides modular `install_*.sh` scripts that can be composed +on top of a slim base image. + +For example, to build a minimal R + geospatial image yourself: + +``` dockerfile +FROM ghcr.io/rocker-org/geospatial:4.4.2 +RUN /rocker_scripts/install_rstudio.sh +``` + +Or to add only what you need: + +``` dockerfile +FROM ghcr.io/rocker/r-ver:4.4.2 +RUN apt-get update && apt-get install -y --no-install-recommends \ + libgdal-dev libgeos-dev libproj-dev \ + && rm -rf /var/lib/apt/lists/* +RUN install2.r sf terra spData +``` + +Key [`rocker-versioned2` +scripts](https://github.com/rocker-org/rocker-versioned2/tree/master/scripts) +to mix and match: + +- [`/rocker_scripts/install_geospatial.sh`](https://github.com/rocker-org/rocker-versioned2/blob/master/scripts/install_geospatial.sh) + — GDAL, PROJ, GEOS, and the core R geospatial packages (sf, terra, + raster, stars, sp, etc.) +- [`/rocker_scripts/install_rstudio.sh`](https://github.com/rocker-org/rocker-versioned2/blob/master/scripts/install_rstudio.sh) + — RStudio Server (uses S6 supervisor, ~300MB) +- [`/rocker_scripts/install_python.sh`](https://github.com/rocker-org/rocker-versioned2/blob/master/scripts/install_python.sh) + — Python 3 + reticulate + venv +- [`/rocker_scripts/install_tidyverse.sh`](https://github.com/rocker-org/rocker-versioned2/blob/master/scripts/install_tidyverse.sh) + — The tidyverse stack +- [`/rocker_scripts/install_quarto.sh`](https://github.com/rocker-org/rocker-versioned2/blob/master/scripts/install_quarto.sh) + — Quarto CLI + +#### Suggested improvements upstream (for the Rocker project) + +1. **Provide a “geospatial-minimal” variant** in `rocker-versioned2` + that omits RStudio Server and the heavy R geospatial package set, + giving just the system libraries (GDAL, PROJ, GEOS, etc.) on top of + `r-ver`. Users could then `install2.r sf terra` on top of a ~1–2GB + base. +2. **Use a smaller base image** (e.g., `debian:bookworm-slim` or + `ubuntu:22.04`) rather than the full `rocker/verse` for images that + don’t need TeX Live, pandoc, or Jupyter. +3. **Prune base image layers** — the [`books/rocker` best + practices](https://rocker-project.org/use/extending.html) already + recommend `--no-install-recommends` and + `rm -rf /var/lib/apt/lists/*`, but the base images themselves could + be rebuilt with multi-stage builds to discard build tools after + package compilation. +4. **Make stripping of RSPM binary libraries opt-out** rather than + opt-in (see + [rocker-versioned2#340](https://github.com/rocker-org/rocker-versioned2/issues/340)) + — this can save 20–30% on R package image size with no observed + runtime cost. +5. **Offer a “pruned” `rocker/geospatial` tag** that has had LaTeX, + pandoc, and Java removed (the largest contributors to image bloat) + for users who only need the geospatial C libraries + R bindings. + +## Building the images locally ### osgeo diff --git a/minimal-slim/Dockerfile b/minimal-slim/Dockerfile new file mode 100644 index 0000000..474dfdc --- /dev/null +++ b/minimal-slim/Dockerfile @@ -0,0 +1,27 @@ +# Slim custom geospatial image built from rocker/r-ver, without RStudio or heavy CLI tools +ARG R_VER=4.4.2 +FROM rocker/r-ver:${R_VER} + +# System dependencies: keep only what sf/terra/spData need at runtime/build time +# Omitting: gdal-bin, netcdf-bin, postgis, Jupyter, pandoc, TeX, Java +RUN apt-get update \ + && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ + libgdal-dev \ + libgeos-dev \ + libproj-dev \ + libudunits2-dev \ + libssl-dev \ + && rm -rf /var/lib/apt/lists/* + +# R packages: geocompx/geocompkg Imports only (sf, terra, spData, units, dplyr, methods, languageserver) +# Using install2.r from rocker-versioned2 style (lightweight, no extra system deps) +ARG NCPUS=-1 +RUN install2.r --error --skipmissing --skipinstalled -n "${NCPUS}" \ + sf terra spData units dplyr methods languageserver \ + && rm -rf /tmp/downloaded_packages + +# Strip binary .so files from RSPM installs +RUN find /usr/local/lib/R/site-library/*/libs -name '*.so' -exec strip --strip-unneeded {} + 2>/dev/null || true + +# Default command +CMD ["R"] diff --git a/minimal/Dockerfile b/minimal/Dockerfile index babf3e4..5ecfdc3 100644 --- a/minimal/Dockerfile +++ b/minimal/Dockerfile @@ -1,25 +1,28 @@ ARG ROCKER_GEOSPATIAL_IMAGE=rocker/geospatial:latest FROM ${ROCKER_GEOSPATIAL_IMAGE} -ARG QUARTO_VERSION=1.9.37 +# Drop packages that are not needed for R geospatial work +# (keep libgdal-dev/libgeos-dev/libproj-dev — needed by sf/terra) +RUN apt-get update \ + && DEBIAN_FRONTEND=noninteractive apt-get purge -y postgis netcdf-bin \ + && apt-get autoremove -y \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* -# Ship editor defaults with the image so downstream Codespaces/devcontainers -# inherit Quarto support even when they only reference the published image. -LABEL devcontainer.metadata='[{"customizations":{"vscode":{"extensions":["quarto.quarto","REditorSupport.r","ms-python.python","ms-toolsai.jupyter"]}}}]' - -RUN R -e "if (!requireNamespace('pak', quietly = TRUE)) install.packages('pak', repos = 'https://cloud.r-project.org/'); pak::pak('geocompx/geocompkg', upgrade = TRUE)" # Install arf — Alternative R Frontend (https://github.com/eitsupi/arf) RUN export ARF_CONSOLE_INSTALL_DIR=/usr/local ARF_CONSOLE_NO_MODIFY_PATH=1 \ && wget -qO /tmp/arf-installer.sh https://github.com/eitsupi/arf/releases/download/v0.3.4/arf-console-installer.sh \ && sh /tmp/arf-installer.sh \ && rm /tmp/arf-installer.sh -# Update quarto to latest stable version: -RUN /rocker_scripts/install_quarto.sh ${QUARTO_VERSION} + # Set RStudio preferences # No inline code: -RUN echo '{' >> /etc/rstudio/rstudio-prefs.json -RUN echo ' "rmd_chunk_output_inline": false' >> /etc/rstudio/rstudio-prefs.json -RUN echo '}' >> /etc/rstudio/rstudio-prefs.json +RUN echo '{' >> /etc/rstudio/rstudio-prefs.json \ + && echo ' "rmd_chunk_output_inline": false' >> /etc/rstudio/rstudio-prefs.json \ + && echo '}' >> /etc/rstudio/rstudio-prefs.json # System-wide shell alias to make 'R' run 'arf' in interactive shells RUN echo "alias R=arf" >> /etc/bash.bashrc + +# Clean up apt caches +RUN rm -rf /var/lib/apt/lists/*