-
Notifications
You must be signed in to change notification settings - Fork 1
/
docker-rstudio-renv.sh
100 lines (85 loc) · 2.98 KB
/
docker-rstudio-renv.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env bash
set -eu
#set variables
r_ver="${1:-"latest"}"
image_name="${image_name:-rstudio_r${r_ver}}"
password="${password:-supersafepassword}"
port="${port:-8787}" #will generate a random one if unavailable
RENV_PATHS_CACHE_HOST="${HOME}/.local/share/renv/cache" #path to renv cache on host, renv default is ~/.local/share/renv/cache
RENV_PATHS_CACHE_CONTAINER="/usr/local/lib/R/renv-cache/" #path to renv cache within the container (dont have to change)
num_threads=$(($(nproc) - 2)) #all cores except 2
#done setting variables
echo "Creating temporary folder for the Dockerfile..."
tmpdir=$(mktemp -dt "${image_name}_XXXXX")
pushd "$tmpdir" > /dev/null
cat << Dockerfile > Dockerfile
FROM rocker/rstudio:${r_ver}
#multithreaded make
ENV MAKEFLAGS="-j ${num_threads} "
ENV CRAN=https://mirrors.dotsrc.org/cran
#install nice-to-have system dependencies for R, and netstat to scan ports
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update -qqy && \\
apt-get install -y --no-install-recommends --no-install-suggests \\
libxml2-dev \\
libcairo2-dev \\
libxt-dev \\
libjpeg-dev \\
net-tools \\
libharfbuzz-dev \\
libfribidi-dev \\
libfreetype6-dev \\
libpng-dev \\
libtiff5-dev \\
libjpeg-dev
#set default renv cache path in container
#change CRAN mirror from https://packagemanager.rstudio.com to AAU mirror
#install renv, and install all packages in the lock file to /usr/local/lib/R/site-library/ in container
RUN echo "RENV_PATHS_CACHE=${RENV_PATHS_CACHE_CONTAINER}" >> /usr/local/lib/R/etc/Renviron.site && \\
echo "options(repos = c(CRAN = 'https://mirrors.dotsrc.org/cran/'), download.file.method = 'libcurl')" >> /usr/local/lib/R/etc/Rprofile.site && \\
R -e "install.packages('renv', Ncpus = ${num_threads})" && \\
R -e "renv::consent(provided = TRUE)"
#silence RStudio warnings about not being able to write dictionary stuff to /root
VOLUME /root
Dockerfile
docker build -t "${image_name}" .
echo "Removing temporary folder..."
popd > /dev/null && rm -rf "$tmpdir"
checkPort() {
randomPort() {
echo $(( ( RANDOM % 60000 ) + 1025 ))
}
local port
local check_port
check_port=${1:-"$(randomPort)"}
while [ -n "$check_port" ]
do
port="$check_port"
check_port=$(docker run --rm --net=host ${image_name} netstat -atn | grep "$port" || :)
if [ -n "$check_port" ]
then
check_port="$(randomPort)"
fi
done
echo "$port"
}
port=$(checkPort "$port")
mkdir -p ${RENV_PATHS_CACHE_HOST}
#launch the container with the host cache mounted in the container
docker run -d \
-e "PASSWORD=${password}" \
-e "RENV_PATHS_CACHE=${RENV_PATHS_CACHE_CONTAINER}" \
-v "${RENV_PATHS_CACHE_HOST}:${RENV_PATHS_CACHE_CONTAINER}" \
-v "$HOME":/home/rstudio \
-p "$port":8787 \
${image_name}
echo
echo "Launch RStudio through a browser at one of these adresses:"
echo "http://127.0.0.1:${port} (this machine only)"
for IP in $(hostname -I)
do
echo "http://${IP}:${port}"
done
echo
echo "Username: rstudio"
echo "Password: ${password}"