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 consistent development environments across machines.
Best use case
create-r-dockerfile is best used when you need a repeatable AI agent workflow instead of a one-off prompt.
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 consistent development environments across machines.
Teams using create-r-dockerfile should expect a more consistent output, faster repeated execution, less prompt rewriting.
When to use this skill
- You want a reusable workflow that can be run more than once with consistent structure.
When not to use this skill
- You only need a quick one-off answer and do not need a reusable workflow.
- You cannot install or maintain the underlying files, dependencies, or repository context.
Installation
Claude Code / Cursor / Codex
Manual Installation
- Download SKILL.md from GitHub
- Place it in
.claude/skills/create-r-dockerfile/SKILL.mdinside your project - Restart your AI agent — it will auto-discover the skill
How create-r-dockerfile Compares
| Feature / Agent | create-r-dockerfile | Standard Approach |
|---|---|---|
| Platform Support | Not specified | Limited / Varies |
| Context Awareness | High | Baseline |
| Installation Complexity | Unknown | N/A |
Frequently Asked Questions
What does this skill do?
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 consistent development environments across machines.
Where can I find the source code?
You can find the source code on GitHub using the link provided at the top of the page.
SKILL.md Source
# Create R Dockerfile
Build a Dockerfile for R projects using rocker base images with proper dependency management.
## When to Use
- Containerizing an R application or analysis
- Creating reproducible R environments
- Deploying R-based services (Shiny, Plumber, MCP server)
- Setting up consistent development environments
## Inputs
- **Required**: R project with dependencies (DESCRIPTION or renv.lock)
- **Required**: Purpose (development, production, or service)
- **Optional**: R version (default: latest stable)
- **Optional**: Additional system libraries needed
## Procedure
### Step 1: Choose Base Image
| Use Case | Base Image | Size |
|----------|-----------|------|
| Minimal R runtime | `rocker/r-ver:4.5.0` | ~800MB |
| With tidyverse | `rocker/tidyverse:4.5.0` | ~1.8GB |
| With RStudio Server | `rocker/rstudio:4.5.0` | ~1.9GB |
| Shiny server | `rocker/shiny-verse:4.5.0` | ~2GB |
**Got:** A base image is selected that matches the project's requirements without unnecessary bloat.
**If fail:** If unsure which image to use, start with `rocker/r-ver` (minimal) and add packages as needed. Check [rocker-org](https://github.com/rocker-org/rocker-versioned2) for the full image catalog.
### Step 2: Write Dockerfile
```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"]
```
**Got:** Dockerfile builds successfully with `docker build -t myproject .`
**If fail:** If the build fails during `apt-get install`, check package names for the target distro (Debian). If `renv::restore()` fails, ensure `renv.lock` and `renv/activate.R` are copied before the restore step.
### Step 3: Create .dockerignore
```
.git
.Rproj.user
.Rhistory
.RData
renv/library
renv/cache
renv/staging
docs/
*.tar.gz
```
**Got:** `.dockerignore` excludes Git history, IDE files, local renv library, and build artifacts from the Docker context.
**If fail:** If the Docker build still copies unwanted files, verify `.dockerignore` is in the same directory as the Dockerfile and uses correct glob patterns.
### Step 4: Build and Test
```bash
docker build -t r-project:latest .
docker run --rm -it r-project:latest R -e "sessionInfo()"
```
**Got:** Container starts with correct R version and all packages available. `sessionInfo()` output confirms the expected R version.
**If fail:** Check build logs for system dependency errors. Add missing `-dev` packages to the `apt-get install` layer.
### Step 5: Optimize for Production
For production deployments, use multi-stage builds:
```dockerfile
# 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 build produces a smaller final image. Runtime stage contains only compiled R packages, not build tools.
**If fail:** If packages fail to load in the runtime stage, ensure the library path in `COPY --from=builder` matches where R installed packages. Check with `R -e ".libPaths()"` in both stages.
## Validation
- [ ] `docker build` completes without errors
- [ ] Container starts and R session works
- [ ] All required packages are available
- [ ] `.dockerignore` excludes unnecessary files
- [ ] Image size is reasonable for the use case
- [ ] Rebuilds are fast when only code changes (layer caching works)
## Pitfalls
- **Missing system dependencies**: R packages with compiled code need `-dev` libraries. Check error messages during `install.packages()`
- **Layer cache invalidation**: Copying all files before installing packages invalidates cache on every code change. Copy lockfile first.
- **Large images**: Use `rm -rf /var/lib/apt/lists/*` after `apt-get install`. Consider multi-stage builds.
- **Timezone issues**: Add `ENV TZ=UTC` or install `tzdata` for timezone-aware operations
- **Running as root**: Add a non-root user for production: `RUN useradd -m appuser && USER appuser`
## Examples
```bash
# 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
```
## Related Skills
- `setup-docker-compose` - orchestrate multiple containers
- `containerize-mcp-server` - special case for MCP R servers
- `optimize-docker-build-cache` - advanced caching strategies
- `manage-renv-dependencies` - renv.lock feeds into Docker builds