From 2517b1fb4341d2c1f2cad6c43376a798ce9e671c Mon Sep 17 00:00:00 2001 From: lukabor Date: Wed, 10 Jun 2026 22:57:31 +0200 Subject: [PATCH] refactor(skills): extract containerize-project from build-* scaffolds - refactor(scaffold): move Docker/compose templates and three-stage doc into new containerize-project skill; build-* skills delegate their Docker step - fix(scaffold): drop source mount from the production compose service - fix(scaffold): add .dockerignore templates so COPY . . excludes .git/.env/data - feat(scaffold): run production stages as non-root app user; pin uv install via COPY --from=ghcr.io/astral-sh/uv:0.7 - docs(README): list containerize-project and add-docker-secrets; remove the now-empty _concepts section Co-Authored-By: Claude Fable 5 --- README.md | 31 +++++----- skills/scaffold/build-py-project/SKILL.md | 16 ++--- skills/scaffold/build-py-project/justfile.tpl | 20 +------ skills/scaffold/build-r-analysis/SKILL.md | 14 +++-- .../build-r-analysis/docker-compose.yml.tpl | 22 ------- skills/scaffold/build-r-analysis/justfile.tpl | 17 +----- skills/scaffold/build-r-package/SKILL.md | 16 ++--- skills/scaffold/build-r-package/justfile.tpl | 17 +----- .../Dockerfile.python.tpl} | 13 ++-- .../Dockerfile.r-analysis.tpl} | 7 +++ .../Dockerfile.r-package.tpl} | 6 ++ skills/scaffold/containerize-project/SKILL.md | 60 +++++++++++++++++++ .../docker-compose.python.yml.tpl} | 3 +- .../docker-compose.r.yml.tpl} | 3 +- .../dockerignore.python.tpl | 17 ++++++ .../containerize-project/dockerignore.r.tpl | 16 +++++ .../containerize-project/justfile-docker.tpl | 18 ++++++ .../three-stage-dockerfile.md | 32 +++++++++- 18 files changed, 212 insertions(+), 116 deletions(-) delete mode 100644 skills/scaffold/build-r-analysis/docker-compose.yml.tpl rename skills/scaffold/{build-py-project/Dockerfile.tpl => containerize-project/Dockerfile.python.tpl} (80%) rename skills/scaffold/{build-r-analysis/Dockerfile.tpl => containerize-project/Dockerfile.r-analysis.tpl} (75%) rename skills/scaffold/{build-r-package/Dockerfile.tpl => containerize-project/Dockerfile.r-package.tpl} (81%) create mode 100644 skills/scaffold/containerize-project/SKILL.md rename skills/scaffold/{build-py-project/docker-compose.yml.tpl => containerize-project/docker-compose.python.yml.tpl} (83%) rename skills/scaffold/{build-r-package/docker-compose.yml.tpl => containerize-project/docker-compose.r.yml.tpl} (85%) create mode 100644 skills/scaffold/containerize-project/dockerignore.python.tpl create mode 100644 skills/scaffold/containerize-project/dockerignore.r.tpl create mode 100644 skills/scaffold/containerize-project/justfile-docker.tpl rename skills/{_concepts => scaffold/containerize-project}/three-stage-dockerfile.md (68%) diff --git a/README.md b/README.md index f9fc134..7bc5d06 100644 --- a/README.md +++ b/README.md @@ -9,8 +9,7 @@ Modeled on [mattpocock/skills](https://github.com/mattpocock/skills). Claude Code discovers skills at `~/.claude/skills//SKILL.md` (flat, one level deep — category subfolders are not scanned). Symlink each skill -individually; the `_concepts/` doc folder lives inside `skills/` and is -not symlinked separately (skills reach it via relative paths): +individually: ```bash mkdir -p ~/.claude/skills @@ -49,19 +48,21 @@ skill list. `justfile`. ### scaffold -- **build-py-project** — uv + Docker (3-stage) + justfile + pyproject. -- **build-r-package** — renv + rocker/r-ver + DESCRIPTION + testthat. -- **build-r-analysis** — renv + rocker/verse + `_targets.R` + Quarto. - -### _concepts -Shared reference docs under `skills/_concepts/` — not skills (no -`SKILL.md`), loaded on demand by skills that link them. The leading -underscore is the "not a skill" marker; the install loop above skips it -because it sits one level shallower than `skills///`. - -- [`skills/_concepts/three-stage-dockerfile.md`](skills/_concepts/three-stage-dockerfile.md) - — base / production / development Docker pattern used by every - `scaffold/build-*` skill. +- **build-py-project** — uv + justfile + pyproject; Docker via + `containerize-project`. +- **build-r-package** — renv + DESCRIPTION + testthat; Docker via + `containerize-project`. +- **build-r-analysis** — renv + `_targets.R` + Quarto; Docker via + `containerize-project`. +- **containerize-project** — three-stage Docker (base/production/ + development) for uv and renv projects: Dockerfile, compose, + `.dockerignore`, justfile recipes. Hardened defaults (non-root + production user, pinned uv, no source mount in prod). The `build-*` + skills delegate their Docker step here; also works standalone on an + existing repo. +- **add-docker-secrets** — build-time (BuildKit secret mounts) and + runtime (`env_file` / compose secrets) credentials without leaking + into image layers. ## Adding a skill diff --git a/skills/scaffold/build-py-project/SKILL.md b/skills/scaffold/build-py-project/SKILL.md index 4844c2b..2cf0ede 100644 --- a/skills/scaffold/build-py-project/SKILL.md +++ b/skills/scaffold/build-py-project/SKILL.md @@ -1,6 +1,6 @@ --- name: build-py-project -description: Scaffold a new Python project with uv for dependency management, a three-stage Docker setup (base/production/development), docker-compose, justfile task runner, and pyproject.toml configured with ruff, ty, and pytest. Materialises files into a target directory and stops on `jj diff` for review before any commit. Use when the user says "new python project", "scaffold python", "create python project", "set up python repo", "bootstrap python", "/build-py-project". +description: Scaffold a new Python project with uv for dependency management, a justfile task runner, and pyproject.toml configured with ruff, ty, and pytest, then delegate the three-stage Docker setup (Dockerfile, docker-compose, .dockerignore) to containerize-project. Materialises files into a target directory and stops on `jj diff` for review before any commit. Use when the user says "new python project", "scaffold python", "create python project", "set up python repo", "bootstrap python", "/build-py-project". --- # build-py-project @@ -26,17 +26,19 @@ description: Scaffold a new Python project with uv for dependency management, a / ├── src//__init__.py ├── tests/fixtures/.gitkeep - ├── Dockerfile ← Dockerfile.tpl - ├── docker-compose.yml ← docker-compose.yml.tpl ├── justfile ← justfile.tpl ├── pyproject.toml ← pyproject.toml.tpl └── .gitignore ← gitignore.tpl ``` -3. **Stop. Do not run `jj describe`.** Tell the user: +3. **Delegate Docker** to + [`/containerize-project`](../containerize-project/SKILL.md) (python + variant): it writes `Dockerfile`, `docker-compose.yml`, + `.dockerignore`, and appends the Docker recipes to the justfile. +4. **Stop. Do not run `jj describe`.** Tell the user: > "Review with `jj diff` (or `git status` + `git diff`). Then run > `just build-dev` to verify the toolchain. Describe the change with > [`/jj-atomic`](../../productivity/jj-atomic/SKILL.md) when satisfied." -4. **Suggest** running [`/write-readme-py`](../../engineering/write-readme-py/SKILL.md) +5. **Suggest** running [`/write-readme-py`](../../engineering/write-readme-py/SKILL.md) after the first successful build to generate the README. ## Placeholders used by templates @@ -56,8 +58,8 @@ description: Scaffold a new Python project with uv for dependency management, a the scaffold lean and let the user grow it. ## See also -- [`../../_concepts/three-stage-dockerfile.md`](../../_concepts/three-stage-dockerfile.md) - — rationale for the base/production/development pattern in `Dockerfile.tpl`. +- [`../containerize-project/SKILL.md`](../containerize-project/SKILL.md) + — owns the Docker templates and the three-stage pattern rationale. - [`../../engineering/dev-python/SKILL.md`](../../engineering/dev-python/SKILL.md) — workflow that this scaffold sets up. - [`../../engineering/write-readme-py/SKILL.md`](../../engineering/write-readme-py/SKILL.md) diff --git a/skills/scaffold/build-py-project/justfile.tpl b/skills/scaffold/build-py-project/justfile.tpl index cb850f8..52e5f7d 100644 --- a/skills/scaffold/build-py-project/justfile.tpl +++ b/skills/scaffold/build-py-project/justfile.tpl @@ -6,24 +6,8 @@ set dotenv-load := false default: @just --list -# ── Docker ────────────────────────────────────────────────────────────── -build-dev: - docker compose build dev - -build-prod: - docker compose build production - -up: - docker compose run --rm dev bash - -up-prod: - docker compose run --rm production - -down: - docker compose down - -rebuild: - docker compose build --no-cache dev +# Docker recipes (build-dev, up, down, …) are appended by +# /containerize-project from its justfile-docker.tpl. # ── Linting & Formatting ──────────────────────────────────────────────── lint: diff --git a/skills/scaffold/build-r-analysis/SKILL.md b/skills/scaffold/build-r-analysis/SKILL.md index a78214c..ee8a1b8 100644 --- a/skills/scaffold/build-r-analysis/SKILL.md +++ b/skills/scaffold/build-r-analysis/SKILL.md @@ -1,6 +1,6 @@ --- name: build-r-analysis -description: Scaffold a new R analysis project with renv for dependency management, three-stage Docker on rocker/verse (base/production/development), docker-compose, justfile, a `_targets.R` workflow skeleton, and Quarto reports under `reports/`. Configured with lintr, styler, testthat, and box. Materialises files and stops on `jj diff` for review before any commit. Use when the user says "new R analysis", "scaffold R project", "create R analysis", "set up R data science project", "new quarto project", "/build-r-analysis". +description: Scaffold a new R analysis project with renv for dependency management, a justfile, a `_targets.R` workflow skeleton, and Quarto reports under `reports/`, then delegate the three-stage Docker setup on rocker/verse (Dockerfile, docker-compose, .dockerignore) to containerize-project. Configured with lintr, styler, testthat, and box. Materialises files and stops on `jj diff` for review before any commit. Use when the user says "new R analysis", "scaffold R project", "create R analysis", "set up R data science project", "new quarto project", "/build-r-analysis". --- # build-r-analysis @@ -26,12 +26,14 @@ description: Scaffold a new R analysis project with renv for dependency manageme ├── reports/index.qmd ← reports-index.qmd.tpl ├── tests/testthat/test-functions.R ← tests-test-functions.R.tpl ├── _targets.R ← _targets.R.tpl - ├── Dockerfile ← Dockerfile.tpl - ├── docker-compose.yml ← docker-compose.yml.tpl ├── justfile ← justfile.tpl └── .gitignore ← gitignore.tpl ``` -3. **Stop. Do not run `jj describe`.** Tell the user: +3. **Delegate Docker** to + [`/containerize-project`](../containerize-project/SKILL.md) + (r-analysis variant): it writes `Dockerfile`, `docker-compose.yml`, + `.dockerignore`, and appends the Docker recipes to the justfile. +4. **Stop. Do not run `jj describe`.** Tell the user: > "Review with `jj diff`. Then `just build-dev` → `just up` → `R -e > 'renv::init()'` to lock dependencies. Then `just run` to execute > the pipeline. Describe with @@ -52,8 +54,8 @@ description: Scaffold a new R analysis project with renv for dependency manageme showing the box-imported pipeline pattern. Replace, don't extend. ## See also -- [`../../_concepts/three-stage-dockerfile.md`](../../_concepts/three-stage-dockerfile.md) - — rationale for `Dockerfile.tpl`. +- [`../containerize-project/SKILL.md`](../containerize-project/SKILL.md) + — owns the Docker templates and the three-stage pattern rationale. - [`../../engineering/dev-r/SKILL.md`](../../engineering/dev-r/SKILL.md) — workflow auto-detects `_targets.R` → R-analysis loop. - [`../../engineering/r-style/SKILL.md`](../../engineering/r-style/SKILL.md) diff --git a/skills/scaffold/build-r-analysis/docker-compose.yml.tpl b/skills/scaffold/build-r-analysis/docker-compose.yml.tpl deleted file mode 100644 index 0ec2e0d..0000000 --- a/skills/scaffold/build-r-analysis/docker-compose.yml.tpl +++ /dev/null @@ -1,22 +0,0 @@ -services: - dev: - build: - context: . - dockerfile: Dockerfile - target: development - container_name: -dev - volumes: - - .:/usr/ - - renv-cache:/root/.cache/R/renv - - production: - build: - context: . - dockerfile: Dockerfile - target: production - container_name: -prod - volumes: - - .:/usr/ - -volumes: - renv-cache: diff --git a/skills/scaffold/build-r-analysis/justfile.tpl b/skills/scaffold/build-r-analysis/justfile.tpl index 19f000e..a656b8c 100644 --- a/skills/scaffold/build-r-analysis/justfile.tpl +++ b/skills/scaffold/build-r-analysis/justfile.tpl @@ -6,21 +6,8 @@ set dotenv-load := false default: @just --list -# ── Docker ────────────────────────────────────────────────────────────── -build-dev: - docker compose build dev - -build-prod: - docker compose build production - -up: - docker compose run --rm dev bash - -down: - docker compose down - -rebuild: - docker compose build --no-cache dev +# Docker recipes (build-dev, up, down, …) are appended by +# /containerize-project from its justfile-docker.tpl. # ── Pipeline ──────────────────────────────────────────────────────────── run: diff --git a/skills/scaffold/build-r-package/SKILL.md b/skills/scaffold/build-r-package/SKILL.md index 319bc62..83e7533 100644 --- a/skills/scaffold/build-r-package/SKILL.md +++ b/skills/scaffold/build-r-package/SKILL.md @@ -1,6 +1,6 @@ --- name: build-r-package -description: Scaffold a new R package with renv for dependency management, a three-stage Docker setup (base/production/development) on rocker/r-ver, docker-compose, justfile task runner, DESCRIPTION, NAMESPACE generation via roxygen2, and testthat ed. 3. Materialises files into a target directory and stops on `jj diff` for review before any commit. Use when the user says "new R package", "scaffold R package", "create R package", "set up R package", "bootstrap R package", "/build-r-package". +description: Scaffold a new R package with renv for dependency management, a justfile task runner, DESCRIPTION, NAMESPACE generation via roxygen2, and testthat ed. 3, then delegate the three-stage Docker setup on rocker/r-ver (Dockerfile, docker-compose, .dockerignore) to containerize-project. Materialises files into a target directory and stops on `jj diff` for review before any commit. Use when the user says "new R package", "scaffold R package", "create R package", "set up R package", "bootstrap R package", "/build-r-package". --- # build-r-package @@ -25,18 +25,20 @@ description: Scaffold a new R package with renv for dependency management, a thr / ├── R/-package.R ├── tests/testthat/test-.R - ├── Dockerfile ← Dockerfile.tpl - ├── docker-compose.yml ← docker-compose.yml.tpl ├── justfile ← justfile.tpl ├── DESCRIPTION ← DESCRIPTION.tpl └── .gitignore ← gitignore.tpl ``` -3. **Stop. Do not run `jj describe`.** Tell the user: +3. **Delegate Docker** to + [`/containerize-project`](../containerize-project/SKILL.md) + (r-package variant): it writes `Dockerfile`, `docker-compose.yml`, + `.dockerignore`, and appends the Docker recipes to the justfile. +4. **Stop. Do not run `jj describe`.** Tell the user: > "Review with `jj diff`. Then run `just build-dev` → `just up` → `R > -e 'renv::init()'` to lock dependencies; then `just document` and > `just check`. Describe the change with > [`/jj-atomic`](../../productivity/jj-atomic/SKILL.md) when satisfied." -4. **Suggest** `/write-readme-py`'s R counterpart once it exists (not +5. **Suggest** `/write-readme-py`'s R counterpart once it exists (not yet — see octobot/README.md *Out of scope*). ## Placeholders used by templates @@ -57,8 +59,8 @@ description: Scaffold a new R package with renv for dependency management, a thr invent stub functions. ## See also -- [`../../_concepts/three-stage-dockerfile.md`](../../_concepts/three-stage-dockerfile.md) - — rationale for `Dockerfile.tpl`. +- [`../containerize-project/SKILL.md`](../containerize-project/SKILL.md) + — owns the Docker templates and the three-stage pattern rationale. - [`../../engineering/dev-r/SKILL.md`](../../engineering/dev-r/SKILL.md) — workflow auto-detects DESCRIPTION → R-package loop. - [`../../engineering/r-style/SKILL.md`](../../engineering/r-style/SKILL.md) diff --git a/skills/scaffold/build-r-package/justfile.tpl b/skills/scaffold/build-r-package/justfile.tpl index ecd9864..60f5140 100644 --- a/skills/scaffold/build-r-package/justfile.tpl +++ b/skills/scaffold/build-r-package/justfile.tpl @@ -6,21 +6,8 @@ set dotenv-load := false default: @just --list -# ── Docker ────────────────────────────────────────────────────────────── -build-dev: - docker compose build dev - -build-prod: - docker compose build production - -up: - docker compose run --rm dev bash - -down: - docker compose down - -rebuild: - docker compose build --no-cache dev +# Docker recipes (build-dev, up, down, …) are appended by +# /containerize-project from its justfile-docker.tpl. # ── Documentation ─────────────────────────────────────────────────────── document: diff --git a/skills/scaffold/build-py-project/Dockerfile.tpl b/skills/scaffold/containerize-project/Dockerfile.python.tpl similarity index 80% rename from skills/scaffold/build-py-project/Dockerfile.tpl rename to skills/scaffold/containerize-project/Dockerfile.python.tpl index 60bf7dc..0727895 100644 --- a/skills/scaffold/build-py-project/Dockerfile.tpl +++ b/skills/scaffold/containerize-project/Dockerfile.python.tpl @@ -1,16 +1,15 @@ FROM python:3.11-slim AS base +# Pinned uv binary — bump the tag deliberately, never use :latest. +COPY --from=ghcr.io/astral-sh/uv:0.7 /uv /uvx /usr/local/bin/ + RUN apt-get update && apt-get install -y --no-install-recommends \ build-essential \ git \ - curl \ ca-certificates \ && rm -rf /var/lib/apt/lists/* \ && mkdir /usr/ -RUN curl -LsSf https://astral.sh/uv/install.sh | sh -ENV PATH="/root/.local/bin:$PATH" - WORKDIR /usr/ ENV UV_LINK_MODE=copy \ @@ -30,10 +29,16 @@ COPY . . RUN --mount=type=cache,target=/root/.cache/uv \ uv sync --frozen --no-dev 2>/dev/null || uv sync --no-dev +RUN useradd --create-home --shell /usr/sbin/nologin app \ + && chown -R app:app /usr/ +USER app + ENV PATH="/opt/venv/bin:$PATH" CMD ["python", "-c", "import ; print(' ready')"] # ── development ──────────────────────────────────────────────────────── +# Runs as root deliberately: the dev service bind-mounts the source tree +# and installs packages interactively; a non-root user fights both. FROM base AS development COPY pyproject.toml uv.lock* ./ diff --git a/skills/scaffold/build-r-analysis/Dockerfile.tpl b/skills/scaffold/containerize-project/Dockerfile.r-analysis.tpl similarity index 75% rename from skills/scaffold/build-r-analysis/Dockerfile.tpl rename to skills/scaffold/containerize-project/Dockerfile.r-analysis.tpl index 37e3b9d..6fe6dbe 100644 --- a/skills/scaffold/build-r-analysis/Dockerfile.tpl +++ b/skills/scaffold/containerize-project/Dockerfile.r-analysis.tpl @@ -22,9 +22,16 @@ RUN R -e "renv::restore()" COPY . . +# tar_make() writes _targets/, so the workdir must be writable by app. +RUN useradd --create-home --shell /usr/sbin/nologin app \ + && chown -R app:app /usr/ +USER app + CMD ["R", "--vanilla", "-e", "targets::tar_make()"] # ── development ──────────────────────────────────────────────────────── +# Runs as root deliberately: the dev service bind-mounts the source tree +# and installs packages interactively; a non-root user fights both. FROM base AS development COPY renv.lock renv.lock diff --git a/skills/scaffold/build-r-package/Dockerfile.tpl b/skills/scaffold/containerize-project/Dockerfile.r-package.tpl similarity index 81% rename from skills/scaffold/build-r-package/Dockerfile.tpl rename to skills/scaffold/containerize-project/Dockerfile.r-package.tpl index 76c813f..06549f2 100644 --- a/skills/scaffold/build-r-package/Dockerfile.tpl +++ b/skills/scaffold/containerize-project/Dockerfile.r-package.tpl @@ -25,10 +25,16 @@ RUN R -e "renv::restore()" COPY . . +RUN useradd --create-home --shell /usr/sbin/nologin app \ + && chown -R app:app /usr/ +USER app + CMD ["R", "--vanilla", "-e", \ "pkgload::load_all(); cat(' ready\\n')"] # ── development ──────────────────────────────────────────────────────── +# Runs as root deliberately: the dev service bind-mounts the source tree +# and installs packages interactively; a non-root user fights both. FROM base AS development COPY renv.lock renv.lock diff --git a/skills/scaffold/containerize-project/SKILL.md b/skills/scaffold/containerize-project/SKILL.md new file mode 100644 index 0000000..39b8117 --- /dev/null +++ b/skills/scaffold/containerize-project/SKILL.md @@ -0,0 +1,60 @@ +--- +name: containerize-project +description: Add a three-stage Docker setup (base/production/development) to a project — Dockerfile, docker-compose.yml, .dockerignore, and Docker recipes in the justfile. Detects the ecosystem from marker files (pyproject.toml → uv on python-slim; DESCRIPTION → renv on rocker/r-ver; _targets.R → renv on rocker/verse). Hardened defaults: non-root production user, pinned package-manager install, no source mount in the production service. Materialises files and stops on `jj diff` for review before any commit. Use when the user says "containerize", "dockerize", "add docker", "/containerize-project", or a scaffold skill (build-py-project, build-r-package, build-r-analysis) delegates Docker setup here. +--- + +# containerize-project + +## When to use +- User says "containerize", "dockerize", "add docker", "/containerize-project". +- A `build-*` scaffold skill reaches its Docker step and delegates here. +- An existing uv or renv project needs the standard three-stage setup. + +## Process + +1. **Detect ecosystem** (in this order; ask if none or several match): + | Marker | Variant | Templates | + |---|---|---| + | `pyproject.toml` | python | `Dockerfile.python.tpl`, `docker-compose.python.yml.tpl`, `dockerignore.python.tpl` | + | `DESCRIPTION` | r-package | `Dockerfile.r-package.tpl`, `docker-compose.r.yml.tpl`, `dockerignore.r.tpl` | + | `_targets.R` | r-analysis | `Dockerfile.r-analysis.tpl`, `docker-compose.r.yml.tpl`, `dockerignore.r.tpl` | +2. **Materialise** with placeholder substitution: + - `Dockerfile` ← variant Dockerfile template + - `docker-compose.yml` ← variant compose template + - `.dockerignore` ← variant dockerignore template +3. **Justfile recipes** from `justfile-docker.tpl`: + - No `justfile` → create one with the standard header + (`set dotenv-load := false`, `default: @just --list`) plus the + Docker section. + - `justfile` exists without a `build-dev` recipe → append the + Docker section. + - `build-dev` already present → leave the justfile alone. +4. **Stop. Do not run `jj describe`.** Tell the user: + > "Review with `jj diff`. Then `just build-dev` to verify the + > toolchain. Describe with `/jj-atomic` when satisfied." + +## Placeholders +- python: `` (kebab-case), `` (snake_case). +- r-package: ``. +- r-analysis: `` (snake_case). + +## Guardrails +- **Never** overwrite an existing `Dockerfile`, `docker-compose.yml`, or + `.dockerignore` without explicit confirmation — show a diff first. +- **Never** put credentials in `ENV`, `ARG`, or `COPY`-ed files — that's + [`add-docker-secrets`](../add-docker-secrets/SKILL.md) territory. +- **Never** add a source mount to the production service — it overrides + the baked image and defeats immutability. +- The development stage runs as root deliberately (bind mounts + + interactive installs); production runs as `app`. Don't "fix" either. +- Keep the pinned uv image tag; never switch to `:latest`. + +## See also +- [three-stage-dockerfile.md](three-stage-dockerfile.md) — rationale for + the base/production/development pattern, caveats, anti-patterns. +- [`../add-docker-secrets/SKILL.md`](../add-docker-secrets/SKILL.md) — + build-time and runtime secrets once the project needs credentials. +- [`../build-py-project/SKILL.md`](../build-py-project/SKILL.md), + [`../build-r-package/SKILL.md`](../build-r-package/SKILL.md), + [`../build-r-analysis/SKILL.md`](../build-r-analysis/SKILL.md) — + scaffolds that delegate their Docker step here. diff --git a/skills/scaffold/build-py-project/docker-compose.yml.tpl b/skills/scaffold/containerize-project/docker-compose.python.yml.tpl similarity index 83% rename from skills/scaffold/build-py-project/docker-compose.yml.tpl rename to skills/scaffold/containerize-project/docker-compose.python.yml.tpl index 50bb88d..35dd9b2 100644 --- a/skills/scaffold/build-py-project/docker-compose.yml.tpl +++ b/skills/scaffold/containerize-project/docker-compose.python.yml.tpl @@ -8,11 +8,10 @@ services: volumes: - .:/usr/ + # No source mount: production runs the baked image as built. production: build: context: . dockerfile: Dockerfile target: production container_name: -prod - volumes: - - .:/usr/ diff --git a/skills/scaffold/build-r-package/docker-compose.yml.tpl b/skills/scaffold/containerize-project/docker-compose.r.yml.tpl similarity index 85% rename from skills/scaffold/build-r-package/docker-compose.yml.tpl rename to skills/scaffold/containerize-project/docker-compose.r.yml.tpl index 40acc06..3a9d760 100644 --- a/skills/scaffold/build-r-package/docker-compose.yml.tpl +++ b/skills/scaffold/containerize-project/docker-compose.r.yml.tpl @@ -9,14 +9,13 @@ services: - .:/usr/ - renv-cache:/root/.cache/R/renv + # No source mount: production runs the baked image as built. production: build: context: . dockerfile: Dockerfile target: production container_name: -prod - volumes: - - .:/usr/ volumes: renv-cache: diff --git a/skills/scaffold/containerize-project/dockerignore.python.tpl b/skills/scaffold/containerize-project/dockerignore.python.tpl new file mode 100644 index 0000000..5828d21 --- /dev/null +++ b/skills/scaffold/containerize-project/dockerignore.python.tpl @@ -0,0 +1,17 @@ +# Keep the build context lean and secrets out of COPY . . +.git +.jj +.env +.env.* +.venv/ +__pycache__/ +*.py[cod] +.coverage +htmlcov/ +.mypy_cache/ +.ruff_cache/ +.pytest_cache/ +dist/ +*.egg-info/ +Dockerfile +docker-compose.yml diff --git a/skills/scaffold/containerize-project/dockerignore.r.tpl b/skills/scaffold/containerize-project/dockerignore.r.tpl new file mode 100644 index 0000000..b52c46a --- /dev/null +++ b/skills/scaffold/containerize-project/dockerignore.r.tpl @@ -0,0 +1,16 @@ +# Keep the build context lean and secrets out of COPY . . +# Data enters containers via the compose source mount, never the image. +.git +.jj +.env +.env.* +.Rproj.user +.Rhistory +.RData +renv/library/ +renv/staging/ +_targets/ +data/raw/ +data/processed/ +Dockerfile +docker-compose.yml diff --git a/skills/scaffold/containerize-project/justfile-docker.tpl b/skills/scaffold/containerize-project/justfile-docker.tpl new file mode 100644 index 0000000..9aa99ed --- /dev/null +++ b/skills/scaffold/containerize-project/justfile-docker.tpl @@ -0,0 +1,18 @@ +# ── Docker ────────────────────────────────────────────────────────────── +build-dev: + docker compose build dev + +build-prod: + docker compose build production + +up: + docker compose run --rm dev bash + +up-prod: + docker compose run --rm production + +down: + docker compose down + +rebuild: + docker compose build --no-cache dev diff --git a/skills/_concepts/three-stage-dockerfile.md b/skills/scaffold/containerize-project/three-stage-dockerfile.md similarity index 68% rename from skills/_concepts/three-stage-dockerfile.md rename to skills/scaffold/containerize-project/three-stage-dockerfile.md index f74ce40..d61de88 100644 --- a/skills/_concepts/three-stage-dockerfile.md +++ b/skills/scaffold/containerize-project/three-stage-dockerfile.md @@ -23,6 +23,9 @@ COPY ./ RUN sync --no-dev --frozen COPY . . RUN sync --no-dev --frozen # final install +RUN useradd --create-home --shell /usr/sbin/nologin app \ + && chown -R app:app /usr/ +USER app CMD ["", ""] # ── development ──────────────────────────────────────────────────────── @@ -48,6 +51,27 @@ the package manager install happen **once**, not twice. manager. `docker compose build --target production` and `--target development` produce two distinct images from one file. +## Hardening defaults + +- **Non-root production user.** Production creates `app` after the final + `COPY` and switches to it; a container escape then lands in an + unprivileged process. Development stays root deliberately — the dev + service bind-mounts the source tree and installs packages + interactively, and a non-root user fights both (UID mismatches on the + mount, no write access to the library path). +- **`.dockerignore` is mandatory.** Without it, `COPY . .` drags `.git`, + `.env`, data directories, and local caches into the image — a secret + leak and a cache-bust in one. Ship it with the Dockerfile, always. +- **Pin the package-manager install.** `curl | sh` fetches whatever is + latest that day; builds stop being reproducible. Copy a pinned binary + instead (`COPY --from=ghcr.io/astral-sh/uv: …`) or install a + pinned version. +- **No source mount on the production service.** A `.:/usr/` + volume on `production` silently overrides the baked image with the + working tree — you are no longer running what you built. +- **Secrets never enter the Dockerfile.** No `ARG`, no `ENV`, no + `COPY`-ed credential files; see `add-docker-secrets`. + ## docker-compose.yml pairing ```yaml @@ -89,6 +113,8 @@ source. ## Used by -- `scaffold/build-py-project` (uv-based) -- `scaffold/build-r-analysis` (renv on rocker/verse) -- `scaffold/build-r-package` (renv on rocker/r-ver) +- `scaffold/containerize-project` — owns the templates implementing this + pattern (python/uv, r-package/rocker-r-ver, r-analysis/rocker-verse). +- `scaffold/build-py-project`, `scaffold/build-r-package`, + `scaffold/build-r-analysis` — delegate their Docker step to + `containerize-project`.