|
| 1 | +# Adding the DevContainer to Your Project |
| 2 | + |
| 3 | +You can add this OCaml devcontainer to your own repository so that contributors (and you) get a working environment via [GitHub Codespaces](https://github.com/features/codespaces), [VS Code Dev Containers](https://code.visualstudio.com/docs/devcontainers/containers), or the [DevContainer CLI](https://github.com/devcontainers/cli). |
| 4 | + |
| 5 | +## Minimal setup |
| 6 | + |
| 7 | +Create `.devcontainer/devcontainer.json` in your project root: |
| 8 | + |
| 9 | +```jsonc |
| 10 | +{ |
| 11 | + "name": "OCaml Development", |
| 12 | + // Pre-built image from Docker Hub |
| 13 | + "image": "cuihtlauac/ocaml-devcontainer:latest", |
| 14 | + |
| 15 | + "customizations": { |
| 16 | + "vscode": { |
| 17 | + "extensions": ["ocamllabs.ocaml-platform"], |
| 18 | + "settings": { |
| 19 | + "ocaml.sandbox": { |
| 20 | + "kind": "opam", |
| 21 | + "switch": "ocaml" |
| 22 | + } |
| 23 | + } |
| 24 | + } |
| 25 | + }, |
| 26 | + |
| 27 | + "postCreateCommand": "opam install . --deps-only -y" |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +Commit this file and push. The "Create codespace" button will appear on your repository's Code tab automatically. |
| 32 | + |
| 33 | +## What each field does |
| 34 | + |
| 35 | +| Field | Purpose | |
| 36 | +|-------|---------| |
| 37 | +| `image` | Pre-built image with OCaml 5.4, dune, ocaml-lsp-server, utop, and [more](../README.md#installed-tools) | |
| 38 | +| `customizations.vscode` | Installs the OCaml extension and configures it for the `ocaml` switch | |
| 39 | +| `postCreateCommand` | Installs your project's opam dependencies on first start. Runs once when the container is created | |
| 40 | + |
| 41 | +## Optional additions |
| 42 | + |
| 43 | +### Dune build cache |
| 44 | + |
| 45 | +Persist the dune cache across container rebuilds so incremental builds stay fast: |
| 46 | + |
| 47 | +```jsonc |
| 48 | +{ |
| 49 | + "mounts": [ |
| 50 | + "source=dune-cache,target=/home/vscode/.cache/dune,type=volume" |
| 51 | + ], |
| 52 | + "remoteEnv": { |
| 53 | + "DUNE_CACHE_ROOT": "/home/vscode/.cache/dune" |
| 54 | + } |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +### Codespace machine size |
| 59 | + |
| 60 | +Request minimum resources for Codespaces (useful for large projects): |
| 61 | + |
| 62 | +```jsonc |
| 63 | +{ |
| 64 | + "hostRequirements": { |
| 65 | + "cpus": 4, |
| 66 | + "memory": "8gb", |
| 67 | + "storage": "32gb" |
| 68 | + } |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +### Claude Code |
| 73 | + |
| 74 | +Add [Claude Code](https://docs.anthropic.com/en/docs/claude-code) as a DevContainer Feature: |
| 75 | + |
| 76 | +```jsonc |
| 77 | +{ |
| 78 | + "features": { |
| 79 | + "ghcr.io/anthropics/devcontainer-features/claude-code:1": {} |
| 80 | + } |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +### Post-start commands |
| 85 | + |
| 86 | +Run commands every time the container starts (not just on creation): |
| 87 | + |
| 88 | +```jsonc |
| 89 | +{ |
| 90 | + "postStartCommand": "dune build" |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +## Full example |
| 95 | + |
| 96 | +A complete `.devcontainer/devcontainer.json` with all optional additions: |
| 97 | + |
| 98 | +```jsonc |
| 99 | +{ |
| 100 | + "name": "OCaml Development", |
| 101 | + "image": "cuihtlauac/ocaml-devcontainer:latest", |
| 102 | + |
| 103 | + "features": { |
| 104 | + "ghcr.io/anthropics/devcontainer-features/claude-code:1": {} |
| 105 | + }, |
| 106 | + |
| 107 | + "customizations": { |
| 108 | + "vscode": { |
| 109 | + "extensions": ["ocamllabs.ocaml-platform"], |
| 110 | + "settings": { |
| 111 | + "ocaml.sandbox": { |
| 112 | + "kind": "opam", |
| 113 | + "switch": "ocaml" |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + }, |
| 118 | + |
| 119 | + "postCreateCommand": "opam install . --deps-only -y", |
| 120 | + |
| 121 | + "mounts": [ |
| 122 | + "source=dune-cache,target=/home/vscode/.cache/dune,type=volume" |
| 123 | + ], |
| 124 | + |
| 125 | + "remoteEnv": { |
| 126 | + "DUNE_CACHE_ROOT": "/home/vscode/.cache/dune" |
| 127 | + }, |
| 128 | + |
| 129 | + "hostRequirements": { |
| 130 | + "cpus": 4, |
| 131 | + "memory": "8gb", |
| 132 | + "storage": "32gb" |
| 133 | + } |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +## For projects with heavy dependencies |
| 138 | + |
| 139 | +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: |
| 140 | + |
| 141 | +```dockerfile |
| 142 | +FROM cuihtlauac/ocaml-devcontainer:latest |
| 143 | +COPY *.opam /tmp/project/ |
| 144 | +RUN cd /tmp/project && opam install . --deps-only -y && rm -rf /tmp/project |
| 145 | +``` |
| 146 | + |
| 147 | +Then reference it in your `devcontainer.json`: |
| 148 | + |
| 149 | +```jsonc |
| 150 | +{ |
| 151 | + "build": { |
| 152 | + "dockerfile": "Dockerfile" |
| 153 | + } |
| 154 | +} |
| 155 | +``` |
| 156 | + |
| 157 | +This bakes your dependencies into the image so container startup is fast. |
| 158 | + |
| 159 | +## Using the devcontainer |
| 160 | + |
| 161 | +Once `.devcontainer/devcontainer.json` is committed: |
| 162 | + |
| 163 | +- **GitHub Codespaces:** Click "Code" > "Codespaces" > "Create codespace" on your repo page |
| 164 | +- **VS Code:** Open the repo folder and click "Reopen in Container" when prompted |
| 165 | +- **CLI:** `devcontainer up --workspace-folder . && devcontainer exec --workspace-folder . bash` |
| 166 | + |
| 167 | +See the [Codespaces guide](SETUP-CODESPACES.md), [VS Code guide](SETUP-VSCODE.md), or [CLI guide](SETUP-DEVCONTAINER-EXEC.md) for details on each workflow. |
0 commit comments