Skip to content

Commit 74677dd

Browse files
committed
chore: Use devcontainers
1 parent 61e0467 commit 74677dd

6 files changed

Lines changed: 203 additions & 68 deletions

File tree

.devcontainer/Dockerfile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
FROM python:3.14-slim
2+
3+
RUN apt-get update && apt-get install -y --no-install-recommends \
4+
build-essential git ca-certificates curl \
5+
&& rm -rf /var/lib/apt/lists/*
6+
7+
RUN curl -fsSL https://deb.nodesource.com/setup_22.x | bash - \
8+
&& apt-get install -y nodejs \
9+
&& rm -rf /var/lib/apt/lists/*
10+
11+
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /usr/local/bin/
12+
13+
ENV UV_NO_SYNC=1
14+
ENV CC=gcc
15+
ENV PYTHONPATH=/workspaces/TorchJD/tests
16+
ENV VIRTUAL_ENV=/opt/venv
17+
ENV PATH=/opt/venv/bin:$PATH
18+
19+
COPY pyproject.toml /tmp/torchjd-install/pyproject.toml
20+
COPY README.md /tmp/torchjd-install/README.md
21+
COPY src/ /tmp/torchjd-install/src/
22+
23+
WORKDIR /tmp/torchjd-install
24+
RUN uv venv /opt/venv \
25+
&& uv pip install --python-version=3.14 -e '.[full]' --group check --group doc --group test --group plot
26+
27+
WORKDIR /workspaces/TorchJD

.devcontainer/devcontainer.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "TorchJD",
3+
"build": {
4+
"dockerfile": "Dockerfile",
5+
"context": ".."
6+
},
7+
"customizations": {
8+
"vscode": {
9+
"extensions": [
10+
"ms-python.python",
11+
"astral-sh.ruff",
12+
"astral-sh.ty"
13+
],
14+
"settings": {
15+
"python.defaultInterpreterPath": "/opt/venv/bin/python",
16+
"python.languageServer": "None",
17+
"[python]": {
18+
"editor.defaultFormatter": "astral-sh.ruff"
19+
},
20+
"ruff.importStrategy": "fromEnvironment",
21+
"ty.importStrategy": "fromEnvironment"
22+
}
23+
}
24+
},
25+
"postCreateCommand": "uv pip install -e '.[full]' --group check --group doc --group test --group plot && uv run pre-commit install"
26+
}

.dockerignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.git
2+
.venv
3+
venv
4+
__pycache__
5+
*.pyc
6+
docs/_build
7+
.pytest_cache
8+
.mypy_cache
9+
.ruff_cache
10+
build/
11+
dist/
12+
*.egg-info/
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: Build Devcontainer
2+
3+
on:
4+
push:
5+
branches: [main]
6+
paths:
7+
- '.devcontainer/Dockerfile'
8+
- 'pyproject.toml'
9+
- '.github/workflows/build-devcontainer.yml'
10+
pull_request:
11+
paths:
12+
- '.devcontainer/Dockerfile'
13+
- 'pyproject.toml'
14+
- '.github/workflows/build-devcontainer.yml'
15+
workflow_dispatch:
16+
17+
jobs:
18+
build:
19+
runs-on: ubuntu-latest
20+
permissions:
21+
packages: write
22+
steps:
23+
- name: Checkout repository
24+
uses: actions/checkout@v6
25+
26+
- name: Log in to GitHub Container Registry
27+
if: github.event_name != 'pull_request'
28+
uses: docker/login-action@v3
29+
with:
30+
registry: ghcr.io
31+
username: ${{ github.actor }}
32+
password: ${{ secrets.GITHUB_TOKEN }}
33+
34+
- name: Build and push devcontainer image
35+
uses: docker/build-push-action@v6
36+
with:
37+
context: .
38+
file: .devcontainer/Dockerfile
39+
push: ${{ github.event_name != 'pull_request' }}
40+
tags: ghcr.io/${{ github.repository }}/devcontainer:latest
41+
cache-from: type=gha
42+
cache-to: type=gha,mode=max

.github/workflows/checks.yml

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,44 @@ jobs:
6464
with:
6565
token: ${{ secrets.CODECOV_TOKEN }}
6666

67-
build-doc:
68-
name: Build and test documentation
67+
build-devcontainer:
68+
name: Build devcontainer image
6969
runs-on: ubuntu-latest
70+
permissions:
71+
packages: write
7072
steps:
7173
- name: Checkout repository
7274
uses: actions/checkout@v6
7375

74-
- name: Set up uv
75-
uses: astral-sh/setup-uv@v7
76+
- name: Log in to GitHub Container Registry
77+
uses: docker/login-action@v3
7678
with:
77-
python-version: '3.14'
79+
registry: ghcr.io
80+
username: ${{ github.actor }}
81+
password: ${{ secrets.GITHUB_TOKEN }}
7882

79-
- uses: ./.github/actions/install-deps
83+
- name: Build and push devcontainer image
84+
uses: docker/build-push-action@v6
8085
with:
81-
groups: doc
86+
context: .
87+
file: .devcontainer/Dockerfile
88+
push: true
89+
tags: ghcr.io/${{ github.repository }}/devcontainer:${{ github.sha }}
90+
cache-from: type=gha
91+
cache-to: type=gha,mode=max
92+
93+
build-doc:
94+
name: Build and test documentation
95+
needs: build-devcontainer
96+
runs-on: ubuntu-latest
97+
container:
98+
image: ghcr.io/${{ github.repository }}/devcontainer:${{ github.sha }}
99+
steps:
100+
- name: Checkout repository
101+
uses: actions/checkout@v6
102+
103+
- name: Sync project installation
104+
run: uv pip install -e '.[full]' --group doc
82105

83106
- name: Build Documentation
84107
working-directory: docs
@@ -90,19 +113,16 @@ jobs:
90113

91114
code-quality:
92115
name: Code quality (ty and pre-commit hooks)
116+
needs: build-devcontainer
93117
runs-on: ubuntu-latest
118+
container:
119+
image: ghcr.io/${{ github.repository }}/devcontainer:${{ github.sha }}
94120
steps:
95121
- name: Checkout repository
96122
uses: actions/checkout@v6
97123

98-
- name: Set up uv
99-
uses: astral-sh/setup-uv@v7
100-
with:
101-
python-version: '3.14'
102-
103-
- uses: ./.github/actions/install-deps
104-
with:
105-
groups: check test plot
124+
- name: Sync project installation
125+
run: uv pip install -e '.[full]' --group check --group test --group plot
106126

107127
- name: Run pre-commit hooks
108128
run: uv run pre-commit run --all-files

CONTRIBUTING.md

Lines changed: 61 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -19,63 +19,72 @@ with the affected code.
1919

2020
## Installation
2121

22-
To work with TorchJD, we suggest you to use [uv](https://docs.astral.sh/uv/). While this is not
23-
mandatory, we only provide installation steps with this tool. You can install it by following their
24-
[installation documentation](https://docs.astral.sh/uv/getting-started/installation/). We also
25-
suggest to use VSCode with the `Python`, `ty` and `ruff` extensions (without `Pylance`).
22+
TorchJD provides a **[devcontainer](https://containers.dev/)** configuration that gives you a
23+
ready-to-use development environment with all dependencies pre-installed. This is the recommended way
24+
to set up your environment.
2625

27-
1) Pre-requisites: Use `uv` to install a Python version compatible with TorchJD and to pin it to the
28-
`TorchJD` folder. From the root of the `TorchJD` repo, run:
29-
```bash
30-
uv python install 3.14.0
31-
uv python pin 3.14.0
32-
```
26+
### Using the devcontainer
3327

34-
2) Create a virtual environment and install the project in it. From the root of `TorchJD`, run:
28+
1. Install [Docker](https://docs.docker.com/get-docker/) and
29+
[VS Code](https://code.visualstudio.com/) with the
30+
[Dev Containers extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers).
31+
2. Clone the repository and open it in VS Code:
3532
```bash
36-
uv venv
37-
CC=gcc uv pip install --python-version=3.14 -e '.[full]' --group check --group doc --group test --group plot
38-
```
39-
If you want to install PyTorch with a different CUDA version (this could be required depending on
40-
your GPU), you'll need to specify an extra index. For instance, for CUDA 12.6, run:
41-
```bash
42-
uv venv
43-
CC=gcc uv pip install --python-version=3.14 -e '.[full]' --group check --group doc --group test --group plot --index-strategy unsafe-best-match --extra-index-url https://download.pytorch.org/whl/cu126
33+
git clone https://github.com/SimplexLab/TorchJD.git
34+
code TorchJD
4435
```
36+
3. When prompted, click **"Reopen in Container"** (or press `Ctrl+Shift+P` and run
37+
*"Dev Containers: Reopen in Container"*). The container will build on first launch and install all
38+
dependencies automatically.
4539

46-
3) Set environment variables:
40+
Alternatively, you can use **[GitHub Codespaces](https://github.com/features/codespaces)** directly
41+
from the repository page — no local setup required.
4742

48-
We need to use `UV_NO_SYNC=1` to prevent `uv` from syncing all the time. This is because by
49-
default, it tries to resolve libraries compatible with the whole range of Python versions
50-
supported by TorchJD, but in reality, we just need an installation compatible with the currently
51-
used Python version. That's also why we specify `--python-version=3.14` when running
52-
`uv pip install`. To follow that recommendation, add the following line to your `.bashrc`:
53-
```bash
54-
export UV_NO_SYNC=1
55-
```
56-
and start a new terminal. The alternative is to use the `--no-sync` flag whenever you run a pip
57-
command that would normally sync (like `uv run`).
43+
The devcontainer includes:
44+
- Python 3.14 with all TorchJD dependencies (including optional ones)
45+
- All development tools: `uv`, `ruff`, `ty`, `pre-commit`, `pytest`
46+
- Pre-configured VS Code extensions and settings
47+
- `PYTHONPATH` set up for the `tests/` folder
5848

59-
Lastly, to run some scripts from the `tests` folder, you'll have to add the `TorchJD/tests`
60-
folder to your `PYTHONPATH`. For that, you should add the following line to your `.bashrc`:
61-
```bash
62-
export PYTHONPATH="$PYTHONPATH:<path_to_TorchJD>/tests"
63-
```
64-
where `<path_to_TorchJD>` is the absolute path to your `TorchJD` repo.
49+
### GPU support
6550

66-
4) Install pre-commit:
67-
```bash
68-
uv run pre-commit install
69-
```
51+
If you have an NVIDIA GPU and want to run tests on CUDA, add the following to
52+
`.devcontainer/devcontainer.json` inside the top-level object:
53+
```json
54+
"runArgs": ["--gpus", "all"]
55+
```
7056

71-
> [!TIP]
72-
> If you're running into issues when `uv` tries to compile `ecos`, make sure that `gcc` is
73-
> installed. Alternatively, you can try to install `clang` or try to use some older Python version
74-
> (3.12) for which `ecos` has provided compiled packages (the list is accessible
75-
> [here](https://pypi.org/project/ecos/#files)).
57+
### CUDA-specific PyTorch installation
7658

77-
> [!TIP]
78-
> The Python version that you should specify in your IDE is `<path-to-TorchJD>/.venv/bin/python`.
59+
By default, the devcontainer installs the CPU-only version of PyTorch. If you need a CUDA version,
60+
run the following inside the container:
61+
```bash
62+
uv pip install --python-version=3.14 -e '.[full]' --group check --group doc --group test --group plot --index-strategy unsafe-best-match --extra-index-url https://download.pytorch.org/whl/cu126
63+
```
64+
(replace `cu126` with your CUDA version).
65+
66+
### Adding LaTeX (for trajectory plotting)
67+
68+
The trajectory plotting scripts require LaTeX. Install it inside the container with:
69+
```bash
70+
sudo apt-get update && sudo apt-get install -y texlive-latex-extra texlive-fonts-recommended dvipng cm-super
71+
```
72+
73+
### Manual setup (without devcontainer)
74+
75+
If you prefer a local setup, you'll need:
76+
- [uv](https://docs.astral.sh/uv/getting-started/installation/)
77+
- Python 3.14 (install via `uv python install 3.14.0`)
78+
- `gcc` (for compiling `ecos`)
79+
80+
Then, from the repo root:
81+
```bash
82+
uv venv
83+
CC=gcc uv pip install --python-version=3.14 -e '.[full]' --group check --group doc --group test --group plot
84+
export UV_NO_SYNC=1
85+
export PYTHONPATH="$PYTHONPATH:$PWD/tests"
86+
uv run pre-commit install
87+
```
7988

8089
> [!TIP]
8190
> In the following commands, you can get rid of the `uv run` prefix if you activate the `venv`
@@ -85,13 +94,12 @@ suggest to use VSCode with the `Python`, `ty` and `ruff` extensions (without `Py
8594

8695
### Clean reinstallation
8796

88-
If you want to update all dependencies or just reinstall from scratch, run the following command
89-
from the root of `TorchJD`:
97+
If you want to update all dependencies or just reinstall from scratch, run the following inside the
98+
container:
9099
```bash
91-
rm -rf .venv
92-
rm -f uv.lock
93-
uv venv
94-
CC=gcc uv pip install --python-version=3.14 -e '.[full]' --group check --group doc --group test --group plot
100+
rm -rf /opt/venv
101+
uv venv /opt/venv
102+
uv pip install --python-version=3.14 -e '.[full]' --group check --group doc --group test --group plot
95103
uv run pre-commit install
96104
```
97105

0 commit comments

Comments
 (0)