|
| 1 | +# Using Ohm.js with Docker |
| 2 | + |
| 3 | +The `ohm:latest` image provides the Ohm compiler CLI in a self-contained environment — no local Node.js or pnpm installation required. |
| 4 | + |
| 5 | +## Commands overview |
| 6 | + |
| 7 | +| Command | Description | |
| 8 | +|-------------------|-------------| |
| 9 | +| `compile` | Compile an `.ohm` grammar file to a `.wasm` module | |
| 10 | +| `generateBundles` | Generate grammar bundles (currently not working) | |
| 11 | +| `shell` | Open a bash shell inside the container | |
| 12 | +| `help` | Print usage information | |
| 13 | + |
| 14 | +Your current directory is mounted at `/local` inside the container, so relative paths to grammar files work as expected. |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +## 1. Running |
| 19 | + |
| 20 | +Pull and run the image directly from Docker Hub: |
| 21 | + |
| 22 | +```sh |
| 23 | +docker run --rm -v $(pwd):/local ohmjs/ohm:latest compile my-grammar.ohm |
| 24 | +``` |
| 25 | + |
| 26 | +### `compile` usage |
| 27 | + |
| 28 | +```sh |
| 29 | +docker run --rm -v $(pwd):/local ohmjs/ohm:latest compile [options] <grammar-file> |
| 30 | +``` |
| 31 | + |
| 32 | +Options: |
| 33 | + |
| 34 | +| Flag | Description | |
| 35 | +|-------------------------------|-------------| |
| 36 | +| `--debug` / `-d` | Enable debug output | |
| 37 | +| `--grammarName` / `-g <name>` | Override the grammar name | |
| 38 | +| `--output` / `-o <file>` | Write output to `<file>` instead of `<grammar-file>.wasm` | |
| 39 | + |
| 40 | +**Example** — compile `arithmetic.ohm` and write the result to `arithmetic.wasm`: |
| 41 | + |
| 42 | +```sh |
| 43 | +docker run --rm -v $(pwd):/local ohmjs/ohm:latest compile -o arithmetic.wasm arithmetic.ohm |
| 44 | +``` |
| 45 | + |
| 46 | +### Getting help |
| 47 | + |
| 48 | +```sh |
| 49 | +docker run --rm ohmjs/ohm:latest help |
| 50 | +``` |
| 51 | + |
| 52 | +--- |
| 53 | + |
| 54 | +## 2. Development |
| 55 | + |
| 56 | +### Minimal Docker setup for macOS |
| 57 | + |
| 58 | +```sh |
| 59 | +brew install colima docker docker-compose docker-credential-helper |
| 60 | +colima start |
| 61 | +``` |
| 62 | + |
| 63 | +Add to `~/.docker/config.json`: |
| 64 | + |
| 65 | +```json |
| 66 | +{ |
| 67 | + "cliPluginsExtraDirs": [ |
| 68 | + "/opt/homebrew/lib/docker/cli-plugins" |
| 69 | + ] |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +> **Note:** If you see `docker-credential-desktop` errors, remove `"credsStore": "desktop"` from `~/.docker/config.json` — it references Docker Desktop, which isn't needed with Colima. |
| 74 | +
|
| 75 | +### Building the image locally |
| 76 | + |
| 77 | +Clone the repository and build the production image with Docker Compose: |
| 78 | + |
| 79 | +```sh |
| 80 | +git clone https://github.com/ohmjs/ohm.git |
| 81 | +cd ohm |
| 82 | +docker compose -f docker/docker-compose.yml build |
| 83 | +``` |
| 84 | + |
| 85 | +This builds the `ohm:latest` image using the `dist` stage of the multi-stage `Dockerfile`, which produces a slim image containing only the compiled packages and their production dependencies. |
| 86 | + |
| 87 | +The `ohm:latest` image is 664 MB and is 99% space efficient (per `wagoodman/dive`). |
| 88 | + |
| 89 | +### Building a development image |
| 90 | + |
| 91 | +The development image uses the `build` stage of the `Dockerfile`, which includes the full source tree, all dev dependencies, and the complete build output. This is useful for iterating on the compiler or debugging build issues. |
| 92 | + |
| 93 | +**Build:** |
| 94 | + |
| 95 | +```sh |
| 96 | +TARGET=build docker compose -f docker/docker-compose.yml build |
| 97 | +``` |
| 98 | + |
| 99 | +This produces the `ohmjs/ohm:development` image. |
| 100 | + |
| 101 | +**Run:** |
| 102 | + |
| 103 | +```sh |
| 104 | +docker run -v $(pwd):/local -it --rm ohmjs/ohm:development shell |
| 105 | +# or |
| 106 | +docker run -v ${PWD}:/local -it --rm ohmjs/ohm:development shell |
| 107 | +``` |
| 108 | + |
| 109 | +The `-v $(pwd):/local` mount makes your current directory available at `/local` inside the container. The `shell` command drops you into a bash session where you can inspect the built artifacts under `/ohm/` or run CLI commands directly. |
| 110 | + |
| 111 | +The `ohm-dev:latest` images is 1.62 GB and is 97% efficient with only 64 MB potentially wasted space. |
| 112 | + |
| 113 | +### Publishing to Docker Hub |
| 114 | + |
| 115 | +Build and push a versioned image to Docker Hub using the git tag as the version: |
| 116 | + |
| 117 | +```sh |
| 118 | +# if not set default to ohmjs (ie docker hub using the ohmjs org) |
| 119 | +export DOCKER_REPO=<custom docker repo> |
| 120 | +# if not set defaults to 'development' |
| 121 | +export VERSION=$(cat packages/runtime/package.json | jq -r '.version') |
| 122 | +# or export VERSION=$(git describe --tag --dirty) |
| 123 | + |
| 124 | +# # it might be necessary (particularly on osx) to create a new builder |
| 125 | +# # the default builder might not support multi-platform builds |
| 126 | +# docker buildx create --use --name ohmjs-builder |
| 127 | +# # might be needed |
| 128 | +# docker buildx inspect --bootstrap |
| 129 | +# # or if already created |
| 130 | +# docker buildx use ohmjs-builder |
| 131 | + |
| 132 | +# generate a person access token at https://app.docker.com/accounts/millergarym/settings/personal-access-tokens |
| 133 | +# assuming DHPAT contains your PAT |
| 134 | +echo $DHPAT | docker login -u <personal username> --password-stdin |
| 135 | +docker buildx bake --push |
| 136 | +``` |
| 137 | + |
| 138 | +`git describe --tag --dirty` produces a version string based on the nearest git tag, appending commit info and a `-dirty` suffix if there are uncommitted changes. |
| 139 | + |
| 140 | +The defaults in `docker-compose.yml` are `DOCKER_REPO=ohmjs` and `VERSION=development`. See [docker-compose.yml](../docker/docker-compose.yml) for details. |
| 141 | + |
| 142 | +## Tips and Tricks |
| 143 | + |
| 144 | +### Image size |
| 145 | + |
| 146 | +To track down file which can be deleted. |
| 147 | + |
| 148 | +**From inside the container** |
| 149 | +```sh |
| 150 | +apt-get update |
| 151 | +apt-get install -y ncdu |
| 152 | +ncdu / |
| 153 | +``` |
| 154 | + |
| 155 | +**Analysing the images** |
| 156 | +```sh |
| 157 | +alias dive="docker run -ti --rm -v /var/run/docker.sock:/var/run/docker.sock docker.io/wagoodman/dive" |
| 158 | +dive ohmjs/ohm:latest |
| 159 | +``` |
0 commit comments