Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.Rhistory
.RData
.Ruserdata
*.html
40 changes: 40 additions & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,46 @@ 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
```

### 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.

A reference `minimal-slim/Dockerfile` is included in this repo showing how to build a lean geospatial R image from `rocker/r-ver` without RStudio or heavy CLI tools.

For example, to build a minimal R + geospatial image yourself:

```Dockerfile
FROM rocker/r-ver:4.4.2
RUN /rocker_scripts/install_geospatial.sh
RUN /rocker_scripts/install_rstudio.sh
```

Or to add only what you need:

```Dockerfile
FROM 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.

## Examples

### osgeo
Expand Down
71 changes: 71 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,77 @@ 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
```

### 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.

A reference `minimal-slim/Dockerfile` is included in this repo showing
how to build a lean geospatial R image from `rocker/r-ver` without
RStudio or heavy CLI tools.

For example, to build a minimal R + geospatial image yourself:

``` dockerfile
FROM rocker/r-ver:4.4.2
RUN /rocker_scripts/install_geospatial.sh
RUN /rocker_scripts/install_rstudio.sh
```

Or to add only what you need:

``` dockerfile
FROM 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.

## Examples

### osgeo
Expand Down
27 changes: 27 additions & 0 deletions minimal-slim/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
33 changes: 21 additions & 12 deletions minimal/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
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"]}}}]'
# Install only the essential geocompkg Imports (not Suggests) to keep the
# image lean. The full geocompkg Suggests set (60+ packages) is available
# via the `suggests` image or users can install on demand.
RUN R -e "if (!requireNamespace('pak', quietly = TRUE)) install.packages('pak', repos = 'https://cloud.r-project.org/'); pak::pak(c('sf','terra','spData','units','dplyr','languageserver','methods'), upgrade = FALSE)"

RUN R -e "if (!requireNamespace('pak', quietly = TRUE)) install.packages('pak', repos = 'https://cloud.r-project.org/'); pak::pak('geocompx/geocompkg', upgrade = TRUE)"
# 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
# Strip binary shared libraries from RSPM packages to reduce final image size
# See https://github.com/rocker-org/rocker-versioned2/issues/340
RUN strip /usr/local/lib/R/site-library/*/libs/*.so

# Set RStudio preferences — no inline code
RUN echo '{' > /etc/rstudio/rstudio-prefs.json \
&& echo ' "rmd_chunk_output_inline": false' >> /etc/rstudio/rstudio-prefs.json \
&& echo '}' >> /etc/rstudio/rstudio-prefs.json

# Clean up apt caches
RUN rm -rf /var/lib/apt/lists/*
Loading