You can add this OCaml devcontainer to your own repository so that contributors (and you) get a working environment via GitHub Codespaces, VS Code Dev Containers, or the DevContainer CLI.
Create .devcontainer/devcontainer.json in your project root:
Commit this file and push. The "Create codespace" button will appear on your repository's Code tab automatically.
| Field | Purpose |
|---|---|
image |
Pre-built image with OCaml 5.4, dune, ocaml-lsp-server, utop, and more |
customizations.vscode |
Installs the OCaml extension and configures it for the ocaml switch |
postCreateCommand |
Installs your project's opam dependencies on first start. Runs once when the container is created |
Persist the dune cache across container rebuilds so incremental builds stay fast:
{
"mounts": [
"source=dune-cache,target=/home/vscode/.cache/dune,type=volume"
],
"remoteEnv": {
"DUNE_CACHE_ROOT": "/home/vscode/.cache/dune"
}
}Request minimum resources for Codespaces (useful for large projects):
{
"hostRequirements": {
"cpus": 4,
"memory": "8gb",
"storage": "32gb"
}
}Add Claude Code as a DevContainer Feature:
{
"features": {
"ghcr.io/anthropics/devcontainer-features/claude-code:1": {}
}
}Run commands every time the container starts (not just on creation):
{
"postStartCommand": "dune build"
}A complete .devcontainer/devcontainer.json with all optional additions:
{
"name": "OCaml Development",
"image": "cuihtlauac/ocaml-devcontainer:latest",
"features": {
"ghcr.io/anthropics/devcontainer-features/claude-code:1": {}
},
"customizations": {
"vscode": {
"extensions": ["ocamllabs.ocaml-platform"],
"settings": {
"ocaml.sandbox": {
"kind": "opam",
"switch": "ocaml"
}
}
}
},
"postCreateCommand": "opam install . --deps-only -y",
"mounts": [
"source=dune-cache,target=/home/vscode/.cache/dune,type=volume"
],
"remoteEnv": {
"DUNE_CACHE_ROOT": "/home/vscode/.cache/dune"
},
"hostRequirements": {
"cpus": 4,
"memory": "8gb",
"storage": "32gb"
}
}If your project has many opam dependencies, postCreateCommand will run on every new container creation, which can be slow. In that case, build a project-specific image instead:
FROM cuihtlauac/ocaml-devcontainer:latest
COPY *.opam /tmp/project/
RUN cd /tmp/project && opam install . --deps-only -y && rm -rf /tmp/projectThen reference it in your devcontainer.json:
{
"build": {
"dockerfile": "Dockerfile"
}
}This bakes your dependencies into the image so container startup is fast.
Once .devcontainer/devcontainer.json is committed:
- GitHub Codespaces: Click "Code" > "Codespaces" > "Create codespace" on your repo page
- VS Code: Open the repo folder and click "Reopen in Container" when prompted
- CLI:
devcontainer up --workspace-folder . && devcontainer exec --workspace-folder . bash
See the Codespaces guide, VS Code guide, or CLI guide for details on each workflow.
{ "name": "OCaml Development", // Pre-built image from Docker Hub "image": "cuihtlauac/ocaml-devcontainer:latest", "customizations": { "vscode": { "extensions": ["ocamllabs.ocaml-platform"], "settings": { "ocaml.sandbox": { "kind": "opam", "switch": "ocaml" } } } }, "postCreateCommand": "opam install . --deps-only -y" }