Skip to contents

Compiles an rcapsule_scan (or project directory path) into a lean, reproducible, layer-cached Dockerfile. Uses pinned Rocker base images (with optional @sha256: digests), installs resolved OS system libraries and detected scientific toolchains (Quarto, TinyTeX, Python, CmdStan, TMB, JAGS, Rust, C++), orders R dependency restoration before project files for optimal Docker cache reuse, drops root privileges, and adds an appropriate HEALTHCHECK and CMD/ENTRYPOINT.

Usage

compile_dockerfile(
  scan,
  output = NULL,
  base_image = NULL,
  r_version = NULL,
  base_digest = NULL,
  platform = NULL,
  cran_repo = NULL,
  use_ppm = TRUE,
  ppm_distro = "jammy",
  ppm_date = "2026-01-15",
  quarto_version = "1.5.57",
  cmdstan_version = "2.35.0",
  user = "capsule",
  workdir = "/capsule",
  port = NULL,
  entrypoint = NULL,
  multi_stage = TRUE,
  include_healthcheck = TRUE,
  allow_unpinned = FALSE
)

Arguments

scan

An rcapsule_scan object from scan_project() or a path to a project directory.

output

Optional file path to write the compiled Dockerfile (e.g., "Dockerfile"). If NULL, returns the compiled spec without writing.

base_image

Optional base Docker image with an explicit pinned tag (defaults to paste0("rocker/r-ver:", r_version)).

r_version

Optional R version string overriding scan$r_version.

base_digest

Optional SHA-256 image digest (e.g., "sha256:...") appended to base_image for immutable image pinning.

platform

Optional target container platform ("linux/amd64", "linux/arm64", or NULL for host default). Useful when building x86_64 HPC or cloud images on Apple Silicon (arm64) hosts.

cran_repo

Optional custom pinned CRAN or Posit Public Package Manager repository URL used when renv.lock is not present. If NULL (default), automatically selects a pinned Posit Package Manager URL controlled by use_ppm, ppm_distro, and ppm_date.

use_ppm

Logical; if TRUE (default) and cran_repo is NULL, uses Posit Public Package Manager Linux binary repositories for dramatically faster reproducible container builds.

ppm_distro

Linux distribution codename for Posit Package Manager binaries ("jammy", "noble", "focal", "bookworm", or "bullseye"). Defaults to "jammy" (Ubuntu 22.04 LTS matching Rocker).

ppm_date

Pinned Posit Package Manager snapshot date ("YYYY-MM-DD"). Defaults to "2026-01-15".

quarto_version

Pinned Quarto CLI version installed when Quarto is detected. Defaults to "1.5.57".

cmdstan_version

Pinned CmdStan version installed when cmdstanr or .stan models are detected. Defaults to "2.35.0".

user

Non-root Linux username for the runtime container. Defaults to "capsule".

workdir

Working directory inside the container. Defaults to "/capsule".

port

Port exposed for Shiny apps (defaults to 3838L when project type is "shiny", otherwise NULL).

entrypoint

Optional override for the runnable entrypoint script or document.

multi_stage

Logical; if TRUE (default), emits a two-stage builder + runtime Dockerfile to minimize final image size.

include_healthcheck

Logical; if TRUE (default), adds a HEALTHCHECK instruction.

allow_unpinned

Logical; if FALSE (default), refuses :latest or untagged base images.

Value

An S3 object of class c("rcapsule_dockerfile", "rcapsule_spec") with fields content, lines, base_image, platform, cran_repo, use_ppm, stages, project_type, path, and scan.

Examples

tmp <- tempfile("rcapsule_df_")
dir.create(tmp)
writeLines(c("library(jsonlite)", "cat('ok\\n')"), file.path(tmp, "run.R"))
df <- compile_dockerfile(tmp)
cat(df$content)
#> # syntax=docker/dockerfile:1.7
#> # Generated by rcapsule 0.6.0
#> # Project: rcapsule_df_1a631147e4e1 (analysis) | R 4.6.2
#> FROM rocker/r-ver:4.6.2 AS builder
#> ENV DEBIAN_FRONTEND=noninteractive \
#>     TZ=UTC \
#>     LANG=C.UTF-8 \
#>     LC_ALL=C.UTF-8 \
#>     R_REPOS=https://packagemanager.posit.co/cran/__linux__/jammy/2026-01-15
#> # 1. OS system dependencies (build + runtime)
#> RUN apt-get update \
#>     && apt-get install -y --no-install-recommends \
#>       ca-certificates \
#>     && rm -rf /var/lib/apt/lists/*
#> WORKDIR /capsule
#> # R package dependencies (cached independently of project code)
#> RUN Rscript -e "install.packages(c('jsonlite'), repos = Sys.getenv('R_REPOS'), Ncpus = parallel::detectCores())"
#> # =============================================================================
#> # Runtime stage: lean non-root execution image
#> # =============================================================================
#> FROM rocker/r-ver:4.6.2 AS runtime
#> LABEL org.opencontainers.image.title="rcapsule_df_1a631147e4e1" \
#>       org.opencontainers.image.revision="uncommitted" \
#>       org.rcapsule.version="0.6.0" \
#>       org.rcapsule.project_type="analysis" \
#>       org.rcapsule.r_version="4.6.2"
#> ENV TZ=UTC \
#>     LANG=C.UTF-8 \
#>     LC_ALL=C.UTF-8
#> # Runtime-only OS libraries
#> RUN apt-get update \
#>     && apt-get install -y --no-install-recommends \
#>       ca-certificates \
#>     && rm -rf /var/lib/apt/lists/*
#> # Copy compiled R libraries and toolchains from builder
#> COPY --from=builder /usr/local/lib/R/site-library /usr/local/lib/R/site-library
#> # Create dedicated non-root runtime user
#> RUN groupadd --system capsule && useradd --system --gid capsule --create-home capsule && mkdir -p /capsule && chown -R capsule:capsule /capsule
#> WORKDIR /capsule
#> COPY --chown=capsule:capsule . /capsule/
#> USER capsule
#> HEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3 \
#>     CMD Rscript -e "quit(status = 0)" || exit 1
#> CMD ["Rscript", "run.R"]
unlink(tmp, recursive = TRUE)