Skip to content

Commit

Permalink
Add rstudio-server to r image as rstudio image
Browse files Browse the repository at this point in the history
  • Loading branch information
remlapmot committed Sep 27, 2024
1 parent bb8e032 commit 437d127
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,10 @@
renv.lock.bak
renv/
.tests.R

# rstudio-server directories
*.config
*.local

# R files
.Rhistory
40 changes: 40 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,43 @@ WORKDIR /workspace
COPY --from=builder /renv /renv
# this will ensure the renv is activated by default
RUN echo 'source("/renv/renv/activate.R")' >> /etc/R/Rprofile.site

#################################################
#
# Add rstudio-server to r image - creating rstudio image
FROM r as rstudio

COPY rstudio-dependencies.txt /root/rstudio-dependencies.txt
COPY rstudio-entrypoint.sh /usr/local/bin/rstudio-entrypoint.sh

# Install rstudio-server (and a few dependencies)
RUN --mount=type=cache,target=/var/cache/apt /root/docker-apt-install.sh /root/rstudio-dependencies.txt &&\
wget -q -O rstudio-server.deb https://download2.rstudio.org/server/focal/amd64/rstudio-server-2024.09.0-375-amd64.deb &&\
apt-get install --no-install-recommends -y ./rstudio-server.deb &&\
# delete the deb
rm rstudio-server.deb &&\
# Setup rstudio user using approach in opensafely-core/research-template-docker
useradd rstudio &&\
# Disable rstudio-server authentication
echo "auth-none=1" >> /etc/rstudio/rserver.conf &&\
# Run the server under the single user account
echo "server-user=rstudio" >> /etc/rstudio/rserver.conf &&\
echo "USER=rstudio" >> /etc/environment &&\
# Give the rstudio user sudo (aka root) permissions
usermod -aG sudo rstudio &&\
echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers &&\
# Add a home directory for the rstudio user
mkdir /home/rstudio &&\
chown -R rstudio:rstudio /home/rstudio/ &&\
# Use renv R packages
# Remember that the second renv library directory /renv/sandbox/R-4.0/x86_64-pc-linux-gnu/9a444a72
# contains 14 symlinks to 14 of the 15 packages in ${R_HOME}/library which is /usr/lib/R/library/
# so that is already setup
echo "R_LIBS_SITE=/renv/lib/R-4.0/x86_64-pc-linux-gnu" >> /usr/lib/R/etc/Renviron.site &&\
# Make entrypoint script executable
chmod +x /usr/local/bin/rstudio-entrypoint.sh &&\
# open RStudio in /workspace
echo "session-default-working-dir=/workspace" >> /etc/rstudio/rsession.conf

ENV USER rstudio
ENV ACTION_EXEC="/usr/local/bin/rstudio-entrypoint.sh"
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,17 @@ Find a previous version at `https://cran.r-project.org/src/contrib/Archive/{PACK
```sh
just add-package PACKAGE@VERSION
```

## Building and publishing the rstudio image

The rstudio image is based on the r image including rstudio-server. To build run

```sh
just build-rstudio
```

And then push the new rstudio image to the GitHub container registry with

```sh
just publish-rstudio
```
6 changes: 5 additions & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ services:
target: add-package
args:
- PACKAGE

rstudio:
extends: r
image: rstudio
build:
target: rstudio
7 changes: 7 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ build:
add-package package:
bash ./add-package.sh {{ package }}

# r image containing rstudio-server
build-rstudio:
docker-compose build --pull rstudio

# test the locally built image
test image="r": build
Expand All @@ -36,3 +39,7 @@ lint:
publish:
docker tag r ghcr.io/opensafely-core/r:latest
docker push ghcr.io/opensafely-core/r:latest

publish-rstudio:
docker tag rstudio ghcr.io/opensafely-core/rstudio:latest
docker push ghcr.io/opensafely-core/rstudio:latest
6 changes: 6 additions & 0 deletions rstudio-dependencies.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
git
libclang-dev
lsb-release
psmisc
sudo
wget
33 changes: 33 additions & 0 deletions rstudio-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash

# On Linux set rstudio user id to same as id of host if rstudio user id not already the same
if [ "$HOSTPLATFORM" = "linux" -a "$(id -u rstudio)" != "$HOSTUID" ]; then
usermod -u $HOSTUID rstudio
fi

# Check for 1 .Rproj file
if [ $(find /workspace -type f -name "*.Rproj" | wc -w) -eq 1 ]; then

# Copy in Git user.name and user.email from copied local-gitconfig from additionally mounted volume
if test -f /home/rstudio/local-gitconfig; then
grep -e "\[user\]" -e "name = *" -e "email = *" /home/rstudio/local-gitconfig >> /home/rstudio/.gitconfig
fi

# Avoid Git error: fatal detected dubious ownership of repository if using Git in container
# Without this the Git pane fails to open when RStudio project opened
echo -e "[safe]\n\tdirectory = \"*\"" >> /home/rstudio/.gitconfig

# Open RStudio project on opening RStudio Server session
echo 'setHook("rstudio.sessionInit", function(newSession) { if (newSession && is.null(rstudioapi::getActiveProject())) rstudioapi::openProject(paste0("/workspace/", list.files(pattern = "Rproj"))) }, action = "append")' >> /home/rstudio/.Rprofile
fi

# Set file line endings as crlf if docker run from Windows
if [ "$HOSTPLATFORM" = "win32" ]; then
echo -e "{\n\t\"line_ending_conversion\": \"windows\"\n}" >> /etc/rstudio/rstudio-prefs.json
fi

# Start RStudio Server session in foreground
# Hence don't use `rstudio-server start` which runs in background
# and attempt to capture std out and err to a metadata log file
mkdir -p /workspace/metadata
exec /usr/lib/rstudio-server/bin/rserver --server-daemonize 0 >> /workspace/metadata/rstudio.log 2>&1

0 comments on commit 437d127

Please sign in to comment.