rstudio / renv

renv: Project environments for R.
https://rstudio.github.io/renv/
MIT License
1.02k stars 155 forks source link

add: Example using docker-compose #599

Open nettoyoussef opened 3 years ago

nettoyoussef commented 3 years ago

Hi all,

Thanks for the work in this awesome package. I use it instead of packrat and it has been wonderful.

Suggestion - add example of docker-compose setup

While the documentation has some examples on using renv with docker, I must admit that I had some trouble converting the usage for a docker-compose setup.

Since it is common to use docker with several containers working together (e.g., one with R, another with Python and yet another with SQL), I think to have such an example in the docs would be useful.

This is a basic template I used that sufficed for my needs, and may inspire you.

docker-compose.yml:

version: '3'

services:

  r_temp:
   build:
     context: ./working_container
     dockerfile: Dockerfile
   env_file: .env  
   volumes:
      - "${RENV_PATHS_CACHE_HOST}:${RENV_PATHS_CACHE}"
   command:  
     #this is a setup for Emacs or CLI usage
     sh -c "R -e 'options(renv.consent = TRUE);renv::restore(lockfile= \"$renv_lock\");' && tail -f /dev/null"
    # for Rstudio substitute "tail..." by 
    # /usr/lib/rstudio-server/bin/rserver --server-daemonize=0

./wroking_container/Dockerfile:

FROM rocker/verse:3.6.1

# create container folder for packages
RUN mkdir -p ~/.local/share/renv/

# install renv
RUN R -e 'install.packages("renv")'

CMD ["/bin/bash"]

env file:

# the path to an renv cache on the host machine
RENV_PATHS_CACHE_HOST=/path/to/host/cache

# the path to the cache within the container
RENV_PATHS_CACHE=/root/.local/share/renv/

# path for renv lockfile used by the container
renv_lock=/path/to/renv.lock
AlbertoFabbri93 commented 5 months ago

This is very useful, please add it to the documentation.