Writing Skills
writing-skills
效率与协作技能,来自 obra/superpowers,查看原文获取完整用途和操作说明。
create-r-dockerfile【部署与运维】Create a Dockerfile for R projects using rocker base images. Covers system dependency installation, R package installation, renv integration, and optimized layer ordering for fast rebuilds. Use when containerizing an R application or analysis, creating reproducible R environments, deploying R-based services (Shiny, Plumber, MCP server), or setting up…
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
Dockerfile for R using rocker + dep mgmt.
| Use | Base | Size |
|---|---|---|
| Min R | rocker/r-ver:4.5.0 | ~800MB |
| Tidyverse | rocker/tidyverse:4.5.0 | ~1.8GB |
| RStudio Svr | rocker/rstudio:4.5.0 | ~1.9GB |
| Shiny | rocker/shiny-verse:4.5.0 | ~2GB |
Got: Base matches reqs, no bloat.
If err: Unsure → rocker/r-ver (min) + add pkgs. See rocker-org catalog.
FROM rocker/r-ver:4.5.0
# Install system dependencies
# Group by purpose for clarity
RUN apt-get update && apt-get install -y \
# HTTP/SSL
libcurl4-openssl-dev \
libssl-dev \
# XML processing
libxml2-dev \
# Git integration
libgit2-dev \
libssh2-1-dev \
# Graphics
libfontconfig1-dev \
libharfbuzz-dev \
libfribidi-dev \
libfreetype6-dev \
libpng-dev \
libtiff5-dev \
libjpeg-dev \
# Utilities
git \
curl \
&& rm -rf /var/lib/apt/lists/*
# Install R packages
# Order: least-changing first for cache efficiency
RUN R -e "install.packages(c( \
'remotes', \
'devtools', \
'renv' \
), repos='https://cloud.r-project.org/')"
# Set working directory
WORKDIR /workspace
# Copy renv files first (cache layer)
COPY renv.lock renv.lock
COPY renv/activate.R renv/activate.R
# Restore packages from lockfile
RUN R -e "renv::restore()"
# Copy project files
COPY . .
# Default command
CMD ["R"]
Got: docker build -t myproject . builds OK.
If err: apt-get install fail → check pkg names (Debian). renv::restore() fail → ensure renv.lock + renv/activate.R copied before restore.
.git
.Rproj.user
.Rhistory
.RData
renv/library
renv/cache
renv/staging
docs/
*.tar.gz
Got: .dockerignore excludes git, IDE, local renv lib, artifacts.
If err: Build still copies unwanted → verify .dockerignore in same dir as Dockerfile, correct glob.
docker build -t r-project:latest .
docker run --rm -it r-project:latest R -e "sessionInfo()"
Got: Container starts, R ver correct, pkgs avail. sessionInfo() confirms.
If err: Check build logs for sys dep err. Add missing -dev pkgs.
Multi-stage:
# Build stage
FROM rocker/r-ver:4.5.0 AS builder
RUN apt-get update && apt-get install -y libcurl4-openssl-dev libssl-dev
COPY renv.lock .
RUN R -e "install.packages('renv'); renv::restore()"
# Runtime stage
FROM rocker/r-ver:4.5.0
COPY --from=builder /usr/local/lib/R/site-library /usr/local/lib/R/site-library
COPY . /app
WORKDIR /app
CMD ["Rscript", "main.R"]
Got: Multi-stage → smaller final img. Runtime = compiled R pkgs only.
If err: Pkgs fail load in runtime → lib path in COPY --from=builder must match R install path. Check: R -e ".libPaths()" in both.
docker build no err.dockerignore excludes junk-dev libs. Check install.packages() err msgsrm -rf /var/lib/apt/lists/* after apt-get install. Multi-stage.ENV TZ=UTC / install tzdata for TZ-aware opsRUN useradd -m appuser && USER appuser# Development container with mounted source
docker run --rm -it -v $(pwd):/workspace r-project:latest R
# Plumber API service
docker run -d -p 8000:8000 r-api:latest
# Shiny app
docker run -d -p 3838:3838 r-shiny:latest
setup-docker-compose — orchestrate multi containerscontainerize-mcp-server — MCP R svrsoptimize-docker-build-cache — cache strategiesmanage-renv-dependencies — renv.lock → Docker