Back to Skills

create-r-dockerfile

pjt222
Updated Yesterday
4 views
17
2
17
View on GitHub
Metaaimcpdesign

About

This skill generates a Dockerfile for R projects using the rocker base images. It handles system dependencies, R package installation, renv integration, and optimizes layer ordering for efficient rebuilds. Use it when containerizing R applications, creating reproducible environments, or deploying R-based services like Shiny or Plumber.

Quick Install

Claude Code

Recommended
Primary
npx skills add pjt222/agent-almanac -a claude-code
Plugin CommandAlternative
/plugin add https://github.com/pjt222/agent-almanac
Git CloneAlternative
git clone https://github.com/pjt222/agent-almanac.git ~/.claude/skills/create-r-dockerfile

Copy and paste this command in Claude Code to install this skill

Documentation

造 R 之 Dockerfile

以 rocker 基礎映像建 R 項目之 Dockerfile,含當之依賴管理。

適用時機

  • 容器化 R 應用或分析
  • 造可重現之 R 環境
  • 部署 R 基礎之服務(Shiny、Plumber、MCP 伺服器)
  • 立一致之開發環境

輸入

  • 必要:含依賴之 R 項目(DESCRIPTION 或 renv.lock)
  • 必要:目的(開發、生產、或服務)
  • 選擇性:R 版本(預設:最新穩定)
  • 選擇性:所需之他系統庫

步驟

步驟一:擇基礎映像

用例基礎映像大小
最小 R 執行rocker/r-ver:4.5.0~800MB
含 tidyverserocker/tidyverse:4.5.0~1.8GB
含 RStudio Serverrocker/rstudio:4.5.0~1.9GB
Shiny 伺服rocker/shiny-verse:4.5.0~2GB

預期: 擇一合項目需、無不必要膨脹之基礎映像。

失敗時: 疑用何映像,始於 rocker/r-ver(最小),按需加套件。全映像目錄見 rocker-org

步驟二:寫 Dockerfile

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"]

預期: Dockerfile 成建:docker build -t myproject .

失敗時: 若於 apt-get install 時敗,察目標發行版(Debian)之套件名。若 renv::restore() 敗,確保 renv.lockrenv/activate.R 於恢復步前已複。

步驟三:造 .dockerignore

.git
.Rproj.user
.Rhistory
.RData
renv/library
renv/cache
renv/staging
docs/
*.tar.gz

預期: .dockerignore 排 Git 歷史、IDE 檔、本地 renv 庫、建構構件於 Docker 脈絡外。

失敗時: 若 Docker 建構仍複不欲檔,驗 .dockerignore 與 Dockerfile 同目錄且用正確 glob 模式。

步驟四:建並測

docker build -t r-project:latest .
docker run --rm -it r-project:latest R -e "sessionInfo()"

預期: 容器啟,R 版本正確,所有套件可得。sessionInfo() 輸出證所期 R 版本。

失敗時: 察建構日誌之系統依賴誤。於 apt-get install 層加缺之 -dev 套件。

步驟五:為生產優化

生產部署用多階段建構:

# 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"]

預期: 多階段建構產較小之終映像。執行階段僅含編譯之 R 套件,無建構工具。

失敗時: 若套件於執行階段載敗,確保 COPY --from=builder 之庫路合 R 裝套件之處。於兩階段以 R -e ".libPaths()" 察之。

驗證

  • docker build 完而無誤
  • 容器啟且 R 會話行
  • 所有所需套件可得
  • .dockerignore 排非必要檔
  • 映像大小合用例
  • 程式改時重建快(層快取有效)

常見陷阱

  • 缺系統依賴:含編譯程式之 R 套件需 -dev 庫。察 install.packages() 時之誤訊
  • 層快取失效:於裝套件前複所有檔令每改程式皆失快取。先複鎖檔
  • 大映像:於 apt-get install 後用 rm -rf /var/lib/apt/lists/*。考慮多階段建構
  • 時區問題:加 ENV TZ=UTC 或裝 tzdata 以行時區感知之操作
  • 以 root 執:生產加非 root 用戶:RUN 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 - 編排多容器
  • containerize-mcp-server - MCP R 伺服之特別案例
  • optimize-docker-build-cache - 進階快取策略
  • manage-renv-dependencies - renv.lock 餵入 Docker 建構

GitHub Repository

pjt222/agent-almanac
Path: i18n/wenyan-lite/skills/create-r-dockerfile
0
agentsagentskillsai-assisted-developmentclaude-codeskillsteams

Related Skills

content-collections

Meta

This skill provides a production-tested setup for Content Collections, a TypeScript-first tool that transforms Markdown/MDX files into type-safe data collections with Zod validation. Use it when building blogs, documentation sites, or content-heavy Vite + React applications to ensure type safety and automatic content validation. It covers everything from Vite plugin configuration and MDX compilation to deployment optimization and schema validation.

View skill

polymarket

Meta

This skill enables developers to build applications with the Polymarket prediction markets platform, including API integration for trading and market data. It also provides real-time data streaming via WebSocket to monitor live trades and market activity. Use it for implementing trading strategies or creating tools that process live market updates.

View skill

creating-opencode-plugins

Meta

This skill helps developers create OpenCode plugins that hook into 25+ event types like commands, files, and LSP operations. It provides the plugin structure, event API specifications, and implementation patterns for JavaScript/TypeScript modules. Use it when you need to intercept, monitor, or extend the OpenCode AI assistant's lifecycle with custom event-driven logic.

View skill

sglang

Meta

SGLang is a high-performance LLM serving framework that specializes in fast, structured generation for JSON, regex, and agentic workflows using its RadixAttention prefix caching. It delivers significantly faster inference, especially for tasks with repeated prefixes, making it ideal for complex, structured outputs and multi-turn conversations. Choose SGLang over alternatives like vLLM when you need constrained decoding or are building applications with extensive prefix sharing.

View skill