Docker

When you develop with BAML, the BAML VScode extension generates a baml_client directory (on every save) with all the generated code you need to use your AI functions in your application.

We recommend you add baml_client to your .gitignore file to avoid committing generated code to your repository, and re-generate the client code when you build and deploy your application.

You could commit the generated code if you’re starting out to not deal with this, just make sure the VSCode extension version matches your baml package dependency version (e.g. baml-py for python and @boundaryml/baml for TS) so there are no compatibility issues.

To build your client you can use the following command. See also baml-cli generate:

1RUN baml-cli generate --from path-to-baml_src

Go: Multi-stage Docker builds

When using Go, the baml-cli generate command downloads the libbaml-cffi native library that BAML needs at runtime. This library is cached to avoid downloading it every time your container runs.

Single-stage builds

For single-stage builds, running baml-cli generate (as shown above) will automatically download and cache libbaml-cffi in your Docker image.

Multi-stage builds

For multi-stage Docker builds, you need to ensure libbaml-cffi is available in your final image. You have two options:

Option 1: Copy the cache directory

Set the BAML_CACHE_DIR environment variable in both stages and copy it to your final image:

1# Build stage
2FROM golang:1.21 AS builder
3ENV BAML_CACHE_DIR=/baml-cache
4WORKDIR /app
5COPY go.mod go.sum ./
6RUN go mod download
7COPY . .
8RUN go install github.com/boundaryml/baml/baml-cli@latest
9RUN baml-cli generate --from baml_src
10RUN go build -o myapp
11
12# Runtime stage
13FROM debian:bookworm-slim
14ENV BAML_CACHE_DIR=/baml-cache
15COPY --from=builder /app/myapp /myapp
16COPY --from=builder /baml-cache /baml-cache
17CMD ["/myapp"]

Option 2: Regenerate in the final stage

Install baml-cli in your final image and run baml-cli --version (or baml-cli generate) to download libbaml-cffi:

1# Build stage
2FROM golang:1.21 AS builder
3WORKDIR /app
4COPY go.mod go.sum ./
5RUN go mod download
6COPY . .
7RUN go install github.com/boundaryml/baml/baml-cli@latest
8RUN baml-cli generate --from baml_src
9RUN go build -o myapp
10
11# Runtime stage
12FROM debian:bookworm-slim
13WORKDIR /app
14COPY --from=builder /app/myapp /myapp
15COPY --from=builder /go/bin/baml-cli /usr/local/bin/baml-cli
16# Download libbaml-cffi into the runtime image
17RUN baml-cli --version
18CMD ["/myapp"]

Customizing the cache directory

By default, BAML downloads libbaml-cffi to a system-specific cache directory. You can control this location using the BAML_CACHE_DIR environment variable:

1ENV BAML_CACHE_DIR=/custom/cache/path

This is particularly useful when you want to explicitly control where the native library is stored in your container.