Skip to content

Commit fa559a3

Browse files
committed
Working version using docker cp rather than mounts
Base on MS devcontainer Jammy image to match OpenSAFELY base image. Install R/Python and their dependencies. 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. These library directories don't contain any symlinks (verified with `find . -type l`).
1 parent af8383d commit fa559a3

3 files changed

Lines changed: 38 additions & 24 deletions

File tree

.devcontainer/devcontainer.json

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,21 @@
22
// README at: https://github.com/devcontainers/templates/tree/main/src/python
33
{
44
"name": "OpenSAFELY",
5-
"image": "mcr.microsoft.com/devcontainers/python:3.10",
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 .devcontainer/postCreate.sh",
1011
"postAttachCommand": "/bin/bash .devcontainer/postAttach.sh",
11-
"containerEnv": {"R_LD_LIBRARY_PATH":"/usr/lib/R-system"},
12+
"forwardPorts": [
13+
8787
14+
],
15+
"portsAttributes": {
16+
"8787": {
17+
"label": "RStudio IDE"
18+
}
19+
},
1220
// Configure tool-specific properties.
1321
"customizations": {
1422
"codespaces": {
@@ -33,7 +41,7 @@
3341
"files.autoSaveDelay": 1000,
3442
"git.autofetch": true,
3543
"python.analysis.extraPaths": [".ehrql/"],
36-
"python.defaultInterpreterPath": "/opt/venv/bin/python3.10",
44+
"python.defaultInterpreterPath": "./venv/bin/python",
3745
"python.terminal.activateEnvInCurrentTerminal": true,
3846
"python.terminal.activateEnvironment": true,
3947
"window.autoDetectColorScheme": true

.devcontainer/postAttach.sh

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1 @@
1-
pip install opensafely
2-
sudo apt update
3-
sudo apt install -y bindfs
4-
docker run -it -d --name ehrql --rm ghcr.io/opensafely-core/ehrql:v1 bash
5-
mount=$(docker inspect ehrql -f '{{.GraphDriver.Data.MergedDir}}')
6-
mkdir -p .ehrql
7-
sudo mount -o bind "$mount/app/ehrql" .ehrql
8-
sudo touch /usr/bin/R
9-
sudo mkdir -p /usr/lib/R
10-
sudo mkdir -p /usr/lib/R-system
11-
sudo mkdir -p /etc/R
12-
docker run -it -d --name r-v2 --rm ghcr.io/opensafely-core/r:v2 bash
13-
mount=$(docker inspect r-v2 -f '{{.GraphDriver.Data.MergedDir}}')
14-
sudo mount --bind -o ro "$mount/usr/bin/R" /usr/bin/R
15-
sudo bindfs -r --resolve-symlinks "$mount/usr/lib/R" /usr/lib/R
16-
sudo bindfs -r --resolve-symlinks "$mount/etc/R" /etc/R
17-
sudo bindfs -r --resolve-symlinks "$mount/lib" /usr/lib/R-system
18-
docker run -it -d --name python-v2 --rm ghcr.io/opensafely-core/python:v2 bash
19-
mount=$(docker inspect python-v2 -f '{{.GraphDriver.Data.MergedDir}}')
20-
sudo mkdir -p /opt/venv
21-
sudo bindfs -r --resolve-symlinks "$mount/opt/venv" /opt/venv
1+
opensafely launch rstudio -p 8787 &

.devcontainer/postCreate.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# install python and dependencies, set up virtualenv
2+
sudo apt update
3+
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
4+
pip install virtualenv opensafely
5+
python3 -m virtualenv .venv
6+
rm -rf .venv/lib
7+
docker container create --name python-v2 ghcr.io/opensafely-core/python:v2
8+
docker cp python-v2:/opt/venv/lib .venv/
9+
docker rm python-v2
10+
11+
# install R and dependencies
12+
echo "deb https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40/" | sudo tee /etc/apt/sources.list.d/cran.list >/dev/null
13+
sudo /usr/lib/apt/apt-helper download-file 'https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc' /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc
14+
sudo apt update
15+
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
16+
wget https://raw.githubusercontent.com/opensafely-core/r-docker/refs/heads/main/scripts/rprofile-site-append-1.R -q -O - | sudo tee -a /usr/lib/R/etc/Rprofile.site >/dev/null
17+
wget https://raw.githubusercontent.com/opensafely-core/r-docker/refs/heads/main/scripts/rprofile-site-append-2.R -q -O - | sudo tee -a /usr/lib/R/etc/Rprofile.site >/dev/null
18+
docker create --name r-v2 --rm ghcr.io/opensafely-core/r:v2
19+
sudo docker cp r-v2:/usr/local/lib/R/site-library /usr/local/lib/R/
20+
sudo chown -R vscode:vscode /usr/local/lib/R/site-library/
21+
22+
23+
#download and extract latest ehrql source
24+
wget https://github.com/opensafely-core/ehrql/archive/main.zip -P .devcontainer
25+
unzip -o .devcontainer/main.zip -d .devcontainer/
26+
rm .devcontainer/main.zip

0 commit comments

Comments
 (0)