Skip to content

Commit 4b9e74d

Browse files
authored
Merge pull request #5 from SumedhSankhe/update-dockerfile
update
2 parents 8b64d52 + 4b7b67b commit 4b9e74d

4 files changed

Lines changed: 193 additions & 107 deletions

File tree

Dockerfile.multistage

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
# ============================================================================
1313
# STAGE 1: Builder - Install dependencies
1414
# ============================================================================
15-
FROM rocker/r-ver:4.4.3 AS builder
15+
FROM rocker/r2u:24.04 AS builder
16+
17+
# Configure renv cache to use the same path as runtime
18+
# This ensures symlinks remain valid when copied to runtime stage
19+
ENV RENV_PATHS_CACHE="/app/renv/.cache"
1620

1721
# Install system dependencies needed for package compilation
1822
RUN apt-get update && apt-get install -y \
@@ -28,7 +32,7 @@ RUN apt-get update && apt-get install -y \
2832
libjpeg-dev \
2933
&& rm -rf /var/lib/apt/lists/*
3034

31-
WORKDIR /build
35+
WORKDIR /app
3236

3337
# OPTIMIZATION 1: Copy only dependency files first
3438
# This creates a cache-friendly layer that only rebuilds when dependencies change
@@ -49,7 +53,10 @@ COPY app.R app.R
4953
# ============================================================================
5054
# STAGE 2: Runtime - Minimal production image
5155
# ============================================================================
52-
FROM rocker/r-ver:4.4.3
56+
FROM rocker/r2u:24.04
57+
58+
# Configure renv cache location to match where packages were installed
59+
ENV RENV_PATHS_CACHE="/app/renv/.cache"
5360

5461
# Install ONLY runtime system dependencies (no build tools)
5562
# Note: Using minimal runtime libraries without -dev packages
@@ -68,24 +75,21 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
6875

6976
WORKDIR /app
7077

71-
# Copy the entire renv library from builder stage
72-
COPY --from=builder /build/renv /app/renv
73-
COPY --from=builder /build/.Rprofile /app/.Rprofile
74-
COPY --from=builder /build/renv.lock /app/renv.lock
75-
76-
# Copy the renv cache (symlinks in renv/library point to this cache)
77-
COPY --from=builder /root/.cache/R/renv /root/.cache/R/renv
78+
# Copy the entire renv library from builder stage (includes cache at /app/renv/.cache)
79+
COPY --from=builder /app/renv /app/renv
80+
COPY --from=builder /app/.Rprofile /app/.Rprofile
81+
COPY --from=builder /app/renv.lock /app/renv.lock
7882

7983
# Copy application code
80-
COPY --from=builder /build/app.R /app/app.R
84+
COPY --from=builder /app/app.R /app/app.R
8185

8286
# Expose Shiny port
8387
EXPOSE 3838
8488

8589
# Run the application
8690
# Note: Use --vanilla to skip .Rprofile and avoid renv activation at runtime
8791
# The renv library path is already in R's search path
88-
CMD ["R", "--vanilla", "-e", ".libPaths('/app/renv/library/linux-ubuntu-noble/R-4.4/x86_64-pc-linux-gnu'); shiny::runApp('/app', host = '0.0.0.0', port = 3838)"]
92+
CMD ["R", "--vanilla", "-e", ".libPaths('/app/renv/library/linux-ubuntu-noble/R-4.5/x86_64-pc-linux-gnu'); shiny::runApp('/app', host = '0.0.0.0', port = 3838)"]
8993

9094
# ============================================================================
9195
# Build and Run Commands:

Dockerfile.single-stage

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
# 4. Larger final image size (~2GB+)
99
# ============================================================================
1010

11-
FROM rocker/r-ver:4.4.3
11+
FROM rocker/r2u:24.04
12+
13+
# Configure renv cache to be inside the app directory
14+
ENV RENV_PATHS_CACHE="/app/renv/.cache"
1215

1316
# Install system dependencies
1417
RUN apt-get update && apt-get install -y \
@@ -41,4 +44,4 @@ EXPOSE 3838
4144
# Run the application
4245
# Note: Use --vanilla to skip .Rprofile and avoid renv activation at runtime
4346
# The renv library path is already in R's search path
44-
CMD ["R", "--vanilla", "-e", ".libPaths('/app/renv/library/linux-ubuntu-noble/R-4.4/x86_64-pc-linux-gnu'); shiny::runApp('/app', host = '0.0.0.0', port = 3838)"]
47+
CMD ["R", "--vanilla", "-e", ".libPaths('/app/renv/library/linux-ubuntu-noble/R-4.5/x86_64-pc-linux-gnu'); shiny::runApp('/app', host = '0.0.0.0', port = 3838)"]

Dockerfile.three-stage

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717
# ============================================================================
1818
# STAGE 1: Base - CRAN packages and build environment
1919
# ============================================================================
20-
FROM rocker/r-ver:4.4.3 AS base
20+
FROM rocker/r2u:24.04 AS base
21+
22+
# Configure renv cache to use the same path as runtime
23+
# This ensures symlinks remain valid when copied to runtime stage
24+
ENV RENV_PATHS_CACHE="/app/renv/.cache"
2125

2226
# Install system dependencies needed for compilation AND runtime
2327
# Build tools are needed here to compile CRAN packages
@@ -35,7 +39,7 @@ RUN apt-get update && apt-get install -y \
3539
libjpeg-dev \
3640
&& rm -rf /var/lib/apt/lists/*
3741

38-
WORKDIR /base
42+
WORKDIR /app
3943

4044
# OPTIMIZATION 1: Copy only dependency files first
4145
# This creates a cache-friendly layer that only rebuilds when CRAN dependencies change
@@ -84,7 +88,10 @@ COPY app.R app.R
8488
# ============================================================================
8589
# STAGE 3: Runtime - Pure application (NO build tools)
8690
# ============================================================================
87-
FROM rocker/r-ver:4.4.3
91+
FROM rocker/r2u:24.04
92+
93+
# Configure renv cache location to match where packages were installed
94+
ENV RENV_PATHS_CACHE="/app/renv/.cache"
8895

8996
# Install ONLY runtime system dependencies (no -dev packages, no build tools)
9097
# Note: Using minimal runtime libraries without -dev packages
@@ -103,13 +110,10 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
103110

104111
WORKDIR /app
105112

106-
# Copy CRAN packages from base stage
107-
COPY --from=base /base/renv /app/renv
108-
COPY --from=base /base/.Rprofile /app/.Rprofile
109-
COPY --from=base /base/renv.lock /app/renv.lock
110-
111-
# Copy the renv cache (symlinks in renv/library point to this cache)
112-
COPY --from=base /root/.cache/R/renv /root/.cache/R/renv
113+
# Copy CRAN packages from base stage (includes cache at /app/renv/.cache)
114+
COPY --from=base /app/renv /app/renv
115+
COPY --from=base /app/.Rprofile /app/.Rprofile
116+
COPY --from=base /app/renv.lock /app/renv.lock
113117

114118
# Copy custom packages from builder stage (if any were installed)
115119
# COPY --from=builder /usr/local/lib/R/site-library/mypackage /usr/local/lib/R/site-library/mypackage
@@ -123,7 +127,7 @@ EXPOSE 3838
123127
# Run the application
124128
# Note: Use --vanilla to skip .Rprofile and avoid renv activation at runtime
125129
# The renv library path is already in R's search path
126-
CMD ["R", "--vanilla", "-e", ".libPaths('/app/renv/library/linux-ubuntu-noble/R-4.4/x86_64-pc-linux-gnu'); shiny::runApp('/app', host = '0.0.0.0', port = 3838)"]
130+
CMD ["R", "--vanilla", "-e", ".libPaths('/app/renv/library/linux-ubuntu-noble/R-4.5/x86_64-pc-linux-gnu'); shiny::runApp('/app', host = '0.0.0.0', port = 3838)"]
127131

128132
# ============================================================================
129133
# Layer Optimization Strategy:

0 commit comments

Comments
 (0)