Restore renv locally without redownloading packages in Shiny Dockerfile

Hello everyone,

I'm trying to build a dockerfile for my shinyapp. There are many libraries to install Shinyapp needs. That's why, you need to define all packages in second RUN layer one by one.

For example:

RUN R -e "install.packages(c('renv', 'shiny', 'tidyverse', ...), repos='https://cloud.r-project.org/')"

However, installing all packages can take too much time. I want to avoid wasting time and use renv in Dockerfile.

When using renv::restore(), it redownloads all packages instead of using them locally. I couldn't figure out how to arrange my dockerfile to avoid this issue.

I already installed all packages with renv in the project directory. How can I avoid redownloading and rebuilding the packages, when building a dockerfile?

FROM openanalytics/r-base

LABEL maintainer  "Tobias Verbeke <tobias.verbeke@openanalytics.eu>"

# https://packagemanager.posit.co/client/#/repos/2/packages/A3
# system libraries of general use
RUN apt-get update && apt-get install -y \
    sudo \
    nano \
    make \
    automake \
    pandoc \
    pandoc-citeproc \
    libicu-dev \
    zlib1g-dev \
    libcurl4-openssl-dev \
    libssl-dev \
    libfontconfig1-dev \
    libfreetype6-dev \
    libfribidi-dev \
    libxml2-dev \
    libharfbuzz-dev \
    libjpeg-dev \
    libpng-dev \
    libtiff-dev \
    libglpk-dev \
    libgmp3-dev \
    libpq-dev \
    libcairo2-dev \
    libxt-dev \
    libssh2-1-dev \
    libssl1.1 \
    libmpfr-dev 
    
# install shiny packages
RUN R -e "install.packages(c('renv'), repos='https://cloud.r-project.org/')"

# Create Directory
RUN mkdir /root/Project

# copy the app to the image
COPY Project /root/Project

# copy all virtual environment files
COPY renv.lock /root/Project/renv.lock
COPY renv /root/Project/renv
COPY .Rprofile /root/Project/.Rprofile

EXPOSE 3838

CMD ["R", "-e", "renv::restore(); shiny::runApp('/root/Project')"]

I'm not sure you ultimately will want to use packages from your local machine in your container, but you should check out the Docker docs on volumes to learn more about the implications.

A couple of other things you might consider:

  • use pre-compiled binary packages from Posit Public Package Manager https://p3m.dev
  • the package restoration should probably be a step in the container build, rather than an argument to the CMD directive
1 Like

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.