Skip to content

Commit 6940b03

Browse files
committed
Use MS devcontainer base, docker cp R/Python libs
Base on MS devcontainer Jammy image to match OpenSAFELY base image. Install R/Python and their dependencies using the respective requirements.txt from the `main` branch on github. This file is not copied to the built docker action images so we cannot copy it from their containers. Use `docker cp` to copy the R/Python library files to the host filesystem which avoids any of the previously seen issues with mounting a running container. However, these library directories don't contain any symlinks (verified with `find . -type l`) so one of the major issues with mounting is not of concern here so commented-out mount-based alternative is provided here which has better performance than the `docker cp` approach.
1 parent fc4b32d commit 6940b03

3 files changed

Lines changed: 50 additions & 4 deletions

File tree

.devcontainer/devcontainer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
// README at: https://github.com/devcontainers/templates/tree/main/src/python
33
{
44
"name": "OpenSAFELY",
5-
"image": "ghcr.io/opensafely-core/research-template:v0",
5+
"image": "mcr.microsoft.com/devcontainers/base:ubuntu-22.04",
66
// Features to add to the dev container. More info: https://containers.dev/features.
77
"features": {
88
"ghcr.io/devcontainers/features/docker-in-docker:2": {}
99
},
10-
"postCreateCommand": "/bin/bash /opt/devcontainer/postCreate.sh ${containerWorkspaceFolder}",
11-
"postAttachCommand": "/bin/bash /opt/devcontainer/postAttach.sh",
10+
"postCreateCommand": "/bin/bash .devcontainer/postCreate.sh",
11+
"postAttachCommand": "/bin/bash .devcontainer/postAttach.sh",
1212
"forwardPorts": [
1313
8787
1414
],
@@ -41,7 +41,7 @@
4141
"files.autoSaveDelay": 1000,
4242
"git.autofetch": true,
4343
"python.analysis.extraPaths": [".devcontainer/ehrql-main/"],
44-
"python.defaultInterpreterPath": "/opt/venv/bin/python",
44+
"python.defaultInterpreterPath": "./venv/bin/python",
4545
"python.terminal.activateEnvInCurrentTerminal": true,
4646
"python.terminal.activateEnvironment": true,
4747
"window.autoDetectColorScheme": true

.devcontainer/postAttach.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
opensafely launch rstudio -p 8787 &

.devcontainer/postCreate.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#download and extract latest ehrql source
2+
wget https://github.com/opensafely-core/ehrql/archive/main.zip -P .devcontainer
3+
unzip -o .devcontainer/main.zip -d .devcontainer/
4+
rm .devcontainer/main.zip
5+
6+
# install python and dependencies, set up virtualenv
7+
sudo apt update
8+
wget https://github.com/opensafely-core/python-docker/raw/refs/heads/main/v2/dependencies.txt -q -O - | sed 's/^#.*//' | sudo xargs apt-get install --yes --no-install-recommends
9+
pip install virtualenv opensafely
10+
python3 -m virtualenv .venv
11+
# copy the docker image virtualenv library to the local virtualenv.
12+
# this could alternatively be done with a bind mount (see below)
13+
rm -rf .venv/lib
14+
docker container create --name python-v2 ghcr.io/opensafely-core/python:v2
15+
docker cp python-v2:/opt/venv/lib .venv/
16+
docker rm python-v2
17+
## bind mount version - faster and less disk space but we need to consider
18+
## implications for `docker pull`/`opensafely pull`
19+
## and what happens if container is stopped/restarted
20+
# rm -rf .venv/lib/*
21+
# docker run -it -d --name python-v2 --rm ghcr.io/opensafely-core/python:v2 bash
22+
# mount=$(docker inspect python-v2 -f '{{.GraphDriver.Data.MergedDir}}')
23+
# mount --bind -o ro "$mount/opt/venv/lib" .venv/lib
24+
25+
26+
# install R and dependencies
27+
docker create --name r-v2 ghcr.io/opensafely-core/r:v2
28+
sudo docker cp r-v2:/etc/apt/sources.list.d/cran.list /etc/apt/sources.list.d/cran.list
29+
sudo docker cp r-v2:/etc/apt/trusted.gpg.d/cran_ubuntu_key.asc /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc
30+
sudo apt update
31+
wget https://raw.githubusercontent.com/opensafely-core/r-docker/refs/heads/main/v2/dependencies.txt -q -O - | sed 's/^#.*//' | sudo xargs apt-get install --yes --no-install-recommends
32+
sudo docker cp r-v2:/usr/lib/R/etc/Rprofile.site /usr/lib/R/etc/Rprofile.site
33+
# copy image R library to local R library
34+
# this could alternatively be done with a bind mount (see below)
35+
docker create --name r-v2 --rm ghcr.io/opensafely-core/r:v2
36+
sudo docker cp r-v2:/usr/local/lib/R/site-library /usr/local/lib/R/
37+
sudo chown -R vscode:vscode /usr/local/lib/R/site-library/
38+
docker rm r-v2
39+
## bind mount version - faster and less disk space but we need to consider
40+
## implications for `docker pull`/`opensafely pull`
41+
## and what happens if container is stopped/restarted
42+
# docker rm r-v2
43+
# docker run -it -d --name r-v2 --rm ghcr.io/opensafely-core/r:v2 bash
44+
# mount=$(docker inspect r-v2 -f '{{.GraphDriver.Data.MergedDir}}')
45+
# sudo mount --bind -o ro "$mount/usr/local/lib/R/site-library" /usr/local/lib/R/

0 commit comments

Comments
 (0)