Deploy BAML applications using Docker with proper client generation
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 TypeScript) so there are no compatibility issues.
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.
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 DirectorySet the BAML_CACHE_DIR environment variable in both stages and copy it to your final image:
# Build stageFROM golang:1.21 AS builderENV BAML_CACHE_DIR=/baml-cacheWORKDIR /appCOPY go.mod go.sum ./RUN go mod downloadCOPY . .RUN go install github.com/boundaryml/baml/baml-cli@latestRUN baml-cli generate --from baml_srcRUN go build -o myapp# Runtime stageFROM debian:bookworm-slimENV BAML_CACHE_DIR=/baml-cacheCOPY --from=builder /app/myapp /myappCOPY --from=builder /baml-cache /baml-cacheCMD ["/myapp"]
Option 2: Regenerate in the Final StageInstall baml-cli in your final image and run baml-cli --version (or baml-cli generate) to download libbaml-cffi:
# Build stageFROM golang:1.21 AS builderWORKDIR /appCOPY go.mod go.sum ./RUN go mod downloadCOPY . .RUN go install github.com/boundaryml/baml/baml-cli@latestRUN baml-cli generate --from baml_srcRUN go build -o myapp# Runtime stageFROM debian:bookworm-slimWORKDIR /appCOPY --from=builder /app/myapp /myappCOPY --from=builder /go/bin/baml-cli /usr/local/bin/baml-cli# Download libbaml-cffi into the runtime imageRUN baml-cli --versionCMD ["/myapp"]
By default, BAML downloads libbaml-cffi to a system-specific cache directory. You can control this location using the BAML_CACHE_DIR environment variable:
ENV 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.
In the directory containing your baml_src/ directory, create a baml.Dockerfile:
FROM node:20WORKDIR /appCOPY baml_src/ .# If you want to pin to a specific version (which we recommend):# RUN npm install -g @boundaryml/baml@VERSIONRUN npm install -g @boundaryml/bamlCMD baml-cli serve --preview --port 2024
To call the BAML server from your laptop (i.e. the host machine), you must use localhost:2024. You may only reach it as baml-over-http:2024 from within another Docker container.