Skip to content

Commit 92289fc

Browse files
authored
add intermediate R build for common packages (#20)
1 parent 1e26f04 commit 92289fc

16 files changed

+151
-67
lines changed

.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ git-wrapper.sh
1212
gpu.Aptfile
1313
gpu.requirements.in
1414
initial-condarc
15+
install_packages.R
1516
ipython_config.py
1617
secrets_helper.sh
1718
requirements.in
@@ -26,6 +27,9 @@ run.sh
2627
!python/base/Aptfile
2728
!python/datascience/Aptfile
2829
!python/noteable/Aptfile
30+
!r/Aptfile
31+
!r/datascience/Aptfile
32+
!r/noteable/Aptfile
2933

3034
!python/base-gpu/gpu.Aptfile
3135
!python/base-gpu/environment.txt
@@ -45,5 +49,10 @@ run.sh
4549
!python/noteable/git_credential_helper.py
4650
!python/noteable/git-wrapper.sh
4751

52+
!r/datascience/install_packages.R
53+
54+
!r/datascience/.Rprofile
4855
!r/noteable/.Rprofile
56+
57+
!r/datascience/requirements.R
4958
!r/noteable/requirements.R

Taskfile.R.yaml

+31-17
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,45 @@ vars:
99
# directly in `cmds: []`, the variables have to be passed in explicitly.
1010

1111
tasks:
12-
core:build:
13-
desc: Build the R 4.x image
14-
cmds:
15-
- >-
16-
docker build R/{{.IDENTIFIER}}/{{.NBL_R_VERSION}} \
17-
--build-arg "NBL_R_VERSION={{.NBL_R_VERSION}}" \
18-
--build-arg "BASE_IMAGE={{.BASE_IMAGE}}" \
19-
--tag "local/kernel-r-{{.NBL_R_VERSION}}-{{.IDENTIFIER}}:dev"
20-
2112
base:copy-files:
2213
desc: Copy files from the R directory to the build directories
2314
cmds:
24-
- task copy-root-files LANGUAGE=R IDENTIFIER={{.IDENTIFIER}} NBL_LANGUAGE_VERSION={{.NBL_R_VERSION}}
25-
- task copy-language-files LANGUAGE=R IDENTIFIER={{.IDENTIFIER}} NBL_LANGUAGE_VERSION={{.NBL_R_VERSION}}
26-
- cp R/noteable/.Rprofile R/noteable/{{.NBL_R_VERSION}}/.Rprofile
27-
- cp R/noteable/requirements.R R/noteable/{{.NBL_R_VERSION}}/requirements.R
15+
- task copy-root-files LANGUAGE=r IDENTIFIER={{.IDENTIFIER}} NBL_LANGUAGE_VERSION={{.NBL_LANGUAGE_VERSION}}
16+
- task copy-language-files LANGUAGE=r IDENTIFIER={{.IDENTIFIER}} NBL_LANGUAGE_VERSION={{.NBL_LANGUAGE_VERSION}}
17+
- cp r/datascience/Aptfile r/datascience/{{.NBL_LANGUAGE_VERSION}}/Aptfile
18+
- cp r/datascience/install_packages.R r/datascience/{{.NBL_LANGUAGE_VERSION}}/install_packages.R
19+
- cp r/datascience/requirements.R r/datascience/{{.NBL_LANGUAGE_VERSION}}/requirements.R
20+
- cp r/noteable/Aptfile r/noteable/{{.NBL_LANGUAGE_VERSION}}/Aptfile
21+
- cp r/noteable/.Rprofile r/noteable/{{.NBL_LANGUAGE_VERSION}}/.Rprofile
22+
- cp r/noteable/requirements.R r/noteable/{{.NBL_LANGUAGE_VERSION}}/requirements.R
23+
2824
base:build:
2925
desc: Build the R 4.x base image after copying required files
3026
cmds:
3127
- task r:base:copy-files IDENTIFIER=base NBL_LANGUAGE_VERSION={{.NBL_R_VERSION}}
32-
- task r:core:build IDENTIFIER=base NBL_R_VERSION={{.NBL_R_VERSION}}
28+
# build the base image
29+
- task build LANGUAGE=r NBL_LANGUAGE_VERSION={{.NBL_R_VERSION}} IDENTIFIER=base -- --build-arg NBL_R_VERSION={{.NBL_R_VERSION}}
30+
31+
datascience:build:
32+
desc: Build the R 4.3.x image with commonly-installed packages
33+
cmds:
34+
# ensure the base image is built first
35+
- task r:base:build IDENTIFIER=datascience NBL_R_VERSION={{.NBL_R_VERSION}}
36+
# copy datascience-specific files to the build directories
37+
- cp r/datascience/Aptfile r/datascience/{{.NBL_R_VERSION}}/Aptfile
38+
- cp r/datascience/install_packages.R r/datascience/{{.NBL_R_VERSION}}/install_packages.R
39+
- cp r/datascience/requirements.R r/datascience/{{.NBL_R_VERSION}}/requirements.R
40+
# build the datascience image off of the base image
41+
- task build LANGUAGE=r NBL_LANGUAGE_VERSION={{.NBL_R_VERSION}} IDENTIFIER=datascience BUILD_TARGET=main -- --build-context base=docker-image://local/kernel-r-{{.NBL_R_VERSION}}-base:dev
3342

3443
noteable:build:
3544
desc: Build the R 4.3.x image with data frame -> DEX support
3645
cmds:
37-
- cp R/noteable/.Rprofile R/noteable/{{.NBL_R_VERSION}}/.Rprofile
38-
- cp R/noteable/requirements.R R/noteable/{{.NBL_R_VERSION}}/requirements.R
39-
- task r:core:build IDENTIFIER=noteable NBL_R_VERSION={{.NBL_R_VERSION}} BASE_IMAGE=local/kernel-r-{{.NBL_R_VERSION}}-base:dev
46+
# ensure the datascience image is built first
47+
- task r:datascience:build IDENTIFIER=noteable NBL_R_VERSION={{.NBL_R_VERSION}}
48+
# copy noteable-specific files to the build directories
49+
- cp r/noteable/Aptfile r/noteable/{{.NBL_R_VERSION}}/Aptfile
50+
- cp r/noteable/.Rprofile r/noteable/{{.NBL_R_VERSION}}/.Rprofile
51+
- cp r/noteable/requirements.R r/noteable/{{.NBL_R_VERSION}}/requirements.R
52+
# build the noteable image off of the datascience image
53+
- task build LANGUAGE=r NBL_LANGUAGE_VERSION={{.NBL_R_VERSION}} IDENTIFIER=noteable BUILD_TARGET=main -- --build-context base=docker-image://local/kernel-r-{{.NBL_R_VERSION}}-datascience:dev

Taskfile.python.yaml

+16-27
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,6 @@ vars:
99
# directly in `cmds: []`, the variables have to be passed in explicitly.
1010

1111
tasks:
12-
core:build:
13-
desc: Build the Python 3.x image
14-
cmds:
15-
- >-
16-
docker build python/{{.IDENTIFIER}}/{{.NBL_PYTHON_VERSION}} \
17-
--build-arg "NBL_PYTHON_VERSION={{.NBL_PYTHON_VERSION}}" \
18-
--build-arg "BASE_IMAGE={{.BASE_IMAGE}}" \
19-
--target "{{.BUILD_TARGET}}" \
20-
--tag "local/kernel-python-{{.NBL_PYTHON_VERSION}}-{{.IDENTIFIER}}{{.TAG_SUFFIX}}:dev"
21-
requires:
22-
vars: [IDENTIFIER, NBL_PYTHON_VERSION]
23-
vars:
24-
BUILD_TARGET: '{{default "base" .BUILD_TARGET}}'
25-
TAG_SUFFIX: '{{default "" .TAG_SUFFIX}}'
26-
2712
base:copy-files:
2813
desc: Copy files from the Python directory to the build directories
2914
cmds:
@@ -74,7 +59,7 @@ tasks:
7459
desc: Build the Python 3.x base image after copying required files
7560
cmds:
7661
- task python:base:copy-files IDENTIFIER=base NBL_PYTHON_VERSION={{.NBL_PYTHON_VERSION}}
77-
- task python:core:build IDENTIFIER=base NBL_PYTHON_VERSION={{.NBL_PYTHON_VERSION}}
62+
- task build LANGUAGE=python NBL_LANGUAGE_VERSION={{.NBL_PYTHON_VERSION}} IDENTIFIER=base
7863

7964
# Base GPU image
8065
base-gpu:lock-dependencies:
@@ -86,14 +71,16 @@ tasks:
8671
base-gpu:build:
8772
desc: Build the Python 3.x image with GPU support after copying required files
8873
cmds:
89-
- task python:base:copy-files IDENTIFIER=base-gpu NBL_PYTHON_VERSION={{.NBL_PYTHON_VERSION}}
74+
# ensure the base image is built first
75+
- task python:base:build IDENTIFIER=datascience NBL_PYTHON_VERSION={{.NBL_PYTHON_VERSION}}
9076
# copy base-gpu specific files
77+
- task python:base:copy-files IDENTIFIER=base-gpu NBL_PYTHON_VERSION={{.NBL_PYTHON_VERSION}}
9178
- cp python/base-gpu/environment.txt python/base-gpu/{{.NBL_PYTHON_VERSION}}/environment.txt
9279
- cp python/base-gpu/gpu.Aptfile python/base-gpu/{{.NBL_PYTHON_VERSION}}/gpu.Aptfile
9380
- cp python/base-gpu/run.sh python/base-gpu/{{.NBL_PYTHON_VERSION}}/run.sh
9481
- cp python/base-gpu/initial-condarc python/base-gpu/{{.NBL_PYTHON_VERSION}}/initial-condarc
95-
# build off of the base image
96-
- task python:core:build IDENTIFIER=base-gpu NBL_PYTHON_VERSION={{.NBL_PYTHON_VERSION}} BASE_IMAGE=local/kernel-python-{{.NBL_PYTHON_VERSION}}-base:dev
82+
# build the base-gpu image off of the base image
83+
- task build LANGUAGE=python NBL_LANGUAGE_VERSION={{.NBL_PYTHON_VERSION}} IDENTIFIER=base-gpu BUILD_TARGET=main -- --build-context base=docker-image://local/kernel-python-{{.NBL_PYTHON_VERSION}}-base:dev
9784

9885
# Datascience image
9986
datascience:lock-dependencies:
@@ -106,8 +93,8 @@ tasks:
10693
cmds:
10794
# ensure the base image is built first
10895
- task python:base:build IDENTIFIER=datascience NBL_PYTHON_VERSION={{.NBL_PYTHON_VERSION}}
109-
# build the datascience image
110-
- task python:core:build IDENTIFIER=datascience NBL_PYTHON_VERSION={{.NBL_PYTHON_VERSION}} BASE_IMAGE=local/kernel-python-{{.NBL_PYTHON_VERSION}}-base:dev
96+
# build the datascience image off of the base image
97+
- task build LANGUAGE=python NBL_LANGUAGE_VERSION={{.NBL_PYTHON_VERSION}} IDENTIFIER=datascience BUILD_TARGET=main -- --build-context base=docker-image://local/kernel-python-{{.NBL_PYTHON_VERSION}}-base:dev
11198

11299
# Datascience GPU image
113100
datascience-gpu:lock-dependencies:
@@ -120,8 +107,8 @@ tasks:
120107
cmds:
121108
# ensure the base-gpu image is built first
122109
- task python:base-gpu:build IDENTIFIER=datascience NBL_PYTHON_VERSION={{.NBL_PYTHON_VERSION}}
123-
# build the datascience-gpu image
124-
- task python:core:build IDENTIFIER=datascience NBL_PYTHON_VERSION={{.NBL_PYTHON_VERSION}} BASE_IMAGE=local/kernel-python-{{.NBL_PYTHON_VERSION}}-base-gpu:dev BUILD_TARGET=gpu TAG_SUFFIX=-gpu
110+
# build the datascience-gpu image off of the base-gpu image
111+
- task build LANGUAGE=python NBL_LANGUAGE_VERSION={{.NBL_PYTHON_VERSION}} IDENTIFIER=datascience BUILD_TARGET=gpu TAG_SUFFIX=-gpu -- --build-context base=docker-image://local/kernel-python-{{.NBL_PYTHON_VERSION}}-base-gpu:dev
125112

126113
# Noteable image
127114
noteable:copy-files:
@@ -144,9 +131,10 @@ tasks:
144131
cmds:
145132
# ensure the datascience image is built first
146133
- task python:datascience:build IDENTIFIER=noteable-gpu NBL_PYTHON_VERSION={{.NBL_PYTHON_VERSION}}
147-
# build the noteable image after copying over noteable-specific files
134+
# copy over noteable-specific files
148135
- task python:noteable:copy-files IDENTIFIER=noteable NBL_PYTHON_VERSION={{.NBL_PYTHON_VERSION}}
149-
- task python:core:build IDENTIFIER=noteable NBL_PYTHON_VERSION={{.NBL_PYTHON_VERSION}} BASE_IMAGE=local/kernel-python-{{.NBL_PYTHON_VERSION}}-datascience:dev
136+
# build the noteable image off of the datascience image
137+
- task build LANGUAGE=python NBL_LANGUAGE_VERSION={{.NBL_PYTHON_VERSION}} IDENTIFIER=noteable BUILD_TARGET=main -- --build-context base=docker-image://local/kernel-python-{{.NBL_PYTHON_VERSION}}-datascience:dev
150138

151139
# Noteable GPU image
152140
noteable-gpu:lock-dependencies:
@@ -159,9 +147,10 @@ tasks:
159147
cmds:
160148
# ensure the datascience-gpu image is built first
161149
- task python:datascience-gpu:build IDENTIFIER=noteable-gpu NBL_PYTHON_VERSION={{.NBL_PYTHON_VERSION}}
162-
# build the noteable-gpu image after copying over noteable-specific files
150+
# copy over noteable-specific files
163151
- task python:noteable:copy-files IDENTIFIER=noteable NBL_PYTHON_VERSION={{.NBL_PYTHON_VERSION}} FILE_PREFIX="gpu."
164-
- task python:core:build IDENTIFIER=noteable NBL_PYTHON_VERSION={{.NBL_PYTHON_VERSION}} BASE_IMAGE=local/kernel-python-{{.NBL_PYTHON_VERSION}}-datascience-gpu:dev BUILD_TARGET=gpu TAG_SUFFIX=-gpu
152+
# build the noteable-gpu image off of the datascience-gpu image
153+
- task build LANGUAGE=python NBL_LANGUAGE_VERSION={{.NBL_PYTHON_VERSION}} IDENTIFIER=noteable BUILD_TARGET=gpu TAG_SUFFIX=-gpu -- --build-context base=docker-image://local/kernel-python-{{.NBL_PYTHON_VERSION}}-datascience-gpu:dev
165154

166155
# convenience functions for building multiple images in parallel
167156
base:lock-all-dependencies:

Taskfile.yaml

+15
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,18 @@ tasks:
2323
generates:
2424
- ./{{.LANGUAGE}}/{{.IDENTIFIER}}/{{.NBL_LANGUAGE_VERSION}}/Aptfile
2525
- ./{{.LANGUAGE}}/{{.IDENTIFIER}}/{{.NBL_LANGUAGE_VERSION}}/run.sh
26+
27+
build:
28+
desc: Build the kernel image with docker buildx
29+
cmds:
30+
- >-
31+
docker buildx build {{.LANGUAGE}}/{{.IDENTIFIER}}/{{.NBL_LANGUAGE_VERSION}} {{.CLI_ARGS}} \
32+
--build-arg "NBL_LANGUAGE_VERSION={{.NBL_LANGUAGE_VERSION}}" \
33+
--target "{{.BUILD_TARGET}}" \
34+
--tag "local/kernel-{{.LANGUAGE}}-{{.NBL_LANGUAGE_VERSION}}-{{.IDENTIFIER}}{{.TAG_SUFFIX}}:{{.TAG}}"
35+
requires:
36+
vars: [LANGUAGE, IDENTIFIER, NBL_LANGUAGE_VERSION]
37+
vars:
38+
BUILD_TARGET: '{{default "base" .BUILD_TARGET}}'
39+
TAG_SUFFIX: '{{default "" .TAG_SUFFIX}}'
40+
TAG: '{{default "dev" .TAG}}'

docker-bake.hcl

+18-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ variable "TIMESTAMP" {
2727
group "rlang_4_3_0" {
2828
targets = [
2929
"rlang_base_4_3_0",
30-
"rlang_noteable_4_3_0"
30+
"rlang_noteable_4_3_0",
31+
"rlang_datascience_4_3_0",
3132
]
3233
}
3334

@@ -306,18 +307,31 @@ target "python_datascience_3_11_gpu" {
306307
# Base R 4.3.0 image
307308
target "rlang_base_4_3_0" {
308309
inherits = ["base"]
309-
context = "R/base/4.3.0"
310+
context = "r/base/4.3.0"
310311
tags = [
311312
"ghcr.io/noteable-io/kernel-r-4.3.0-base:${TAG}"
312313
]
313314
}
314315

316+
# R 4.3.0 Datascience variant
317+
target "rlang_datascience_4_3_0" {
318+
context = "r/datascience/4.3.0"
319+
contexts = {
320+
base = "target:rlang_base_4_3_0"
321+
}
322+
target = "main"
323+
tags = [
324+
"ghcr.io/noteable-io/kernel-r-4.3.0-datascience:${TAG}"
325+
]
326+
}
327+
315328
# R 4.3.0 Noteable variant
316329
target "rlang_noteable_4_3_0" {
317-
context = "R/noteable/4.3.0"
330+
context = "r/noteable/4.3.0"
318331
contexts = {
319-
base = "target:rlang_base_4_3_0"
332+
base = "target:rlang_datascience_4_3_0"
320333
}
334+
target = "main"
321335
tags = [
322336
"ghcr.io/noteable-io/kernel-r-4.3.0-noteable:${TAG}"
323337
]

R/Aptfile renamed to r/Aptfile

File renamed without changes.

R/base/4.3.0/Dockerfile renamed to r/base/4.3.0/Dockerfile

+3-12
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# - no git, secrets, SQL, extensions, etc
66
# ---
77
ARG NBL_R_VERSION=4.3.0
8-
FROM r-base:${NBL_R_VERSION}
8+
FROM r-base:${NBL_R_VERSION} as base
99

1010
# User/group setup
1111
USER root
@@ -22,7 +22,8 @@ RUN groupadd --gid 4004 noteable && \
2222
--gid noteable noteable \
2323
--home-dir /srv/noteable && \
2424
chown --recursive noteable:noteable /srv/noteable && \
25-
mkdir -p /etc/noteable && chown noteable:noteable /etc/noteable
25+
mkdir -p /etc/noteable && chown noteable:noteable /etc/noteable && \
26+
chown --recursive noteable:noteable /usr/local/lib/R/site-library
2627

2728
# Install tini to manage passing signals to the child kernel process
2829
ENV TINI_VERSION v0.19.0
@@ -35,17 +36,7 @@ COPY apt-install /usr/bin/
3536
COPY Aptfile .
3637
RUN /usr/bin/apt-install Aptfile
3738

38-
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
39-
RUN wget -qO- https://micromamba.snakepit.net/api/micromamba/linux-64/latest | tar -xvj bin/micromamba && \
40-
./bin/micromamba shell init -s bash -p ~/micromamba
41-
4239
USER noteable
43-
RUN micromamba create --name noteable-venv \
44-
-c conda-forge \
45-
-y \
46-
r="${NBL_R_VERSION}"
47-
# make subsequent RUN commands use the virtualenv:
48-
SHELL ["micromamba", "run", "-n", "noteable-venv", "/bin/bash", "-c"]
4940

5041
# hadolint ignore=SC2239
5142
RUN R -e "install.packages('IRkernel', repos='http://cran.us.r-project.org')"

r/datascience/4.3.0/Dockerfile

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# syntax = docker/dockerfile:1.5
2+
# Datascience build: adds packages based on the Jupyter r-notebook dependencies
3+
# (ref: https://github.com/jupyter/docker-stacks/blob/main/r-notebook/Dockerfile)
4+
# hadolint ignore=DL3006
5+
FROM base as main
6+
7+
USER root
8+
9+
# R package dependencies
10+
# hadolint ignore=DL3045
11+
COPY Aptfile .
12+
RUN /usr/bin/apt-install Aptfile
13+
14+
USER noteable
15+
16+
# install R packages
17+
COPY requirements.R /tmp/requirements.R
18+
COPY install_packages.R /tmp/install_packages.R
19+
RUN R -e "source('/tmp/install_packages.R')"

r/datascience/Aptfile

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
libgdal-dev

r/datascience/install_packages.R

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
packages <- readLines("/tmp/requirements.R")
2+
install.packages(packages, repos='http://cran.us.r-project.org')

r/datascience/requirements.R

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
arrow
2+
caret
3+
crayon
4+
devtools
5+
dplyr
6+
e1071
7+
forecast
8+
ggplot2
9+
ggpubr
10+
hexbin
11+
htmltools
12+
htmlwidgets
13+
nycflights13
14+
randomforest
15+
rcurl
16+
rgdal
17+
rmarkdown
18+
rodbc
19+
rsqlite
20+
shiny
21+
tidymodels
22+
tidyverse
23+
unixodbc
File renamed without changes.

R/noteable/4.3.0/Dockerfile renamed to r/noteable/4.3.0/Dockerfile

+8-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22
# Noteable build: adds packages to enable Noteable-specific functionality:
33
# - DEX support (via .Rprofile)
44
# hadolint ignore=DL3006
5-
FROM base
5+
FROM base as main
66

7-
USER noteable
7+
USER root
8+
9+
# install python for reticulate support
10+
# hadolint ignore=DL3045
11+
COPY Aptfile .
12+
RUN /usr/bin/apt-install Aptfile
813

9-
# Install python to use with Reticulate
10-
RUN micromamba install python=3.9 -y -c conda-forge
14+
USER noteable
1115

1216
# R package dependencies and py_install
1317
COPY requirements.R /tmp/requirements.R

r/noteable/Aptfile

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
python3
2+
python3-venv
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
install.packages('reticulate', repos='http://cran.us.r-project.org')
22
library(reticulate)
3+
# for whatever reason, reticulate doesn't work with the default python installation
4+
install_miniconda()
35
# Python packages to be used in R via reticulate
46
# ref: https://rstudio.github.io/reticulate/articles/python_packages.html
57
py_install('pandas==1.5.3', pip=TRUE)

R/run.sh renamed to r/run.sh

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ set -o pipefail
33
set -o nounset
44
set -o errexit
55

6-
echo "Local time: $(date)"
7-
86
set -x
7+
echo "Local time: $(date)"
98

109
connection_file=/tmp/connection_file.json
1110
cp /etc/noteable/connections/connection_file.json ${connection_file}
@@ -20,4 +19,4 @@ echo "Done injecting Secrets, turning echoing back on"
2019
set -x
2120

2221
echo "Starting R kernel"
23-
micromamba run -n noteable-venv R --slave -e "IRkernel::main()" --args ${connection_file}
22+
exec R --slave -e "IRkernel::main()" --args ${connection_file}

0 commit comments

Comments
 (0)