This project demonstrates how to create a container image using Bazel and rules_oci, starting from a DHI Alpine base 3.22 image and copying a local file into the container.
.
├── MODULE.bazel # Modern Bazel dependency management (bzlmod)
├── WORKSPACE # Empty WORKSPACE file for compatibility
├── .bazelrc # Bazel configuration
├── .gitignore # Git ignore file for build artifacts
├── BUILD.bazel # Main build targets
├── hello.txt # Sample file to copy into container
├── platforms/
│ └── BUILD.bazel # Platform definitions
├── demo.sh # Demo script to build and show usage
└── README.md # This file
- Bazel version 8.0 or later
- Docker (optional, for testing the built image)
- Base Image: Uses DHI Alpine base 3.22 as the base image
- File Copy: Copies the local
hello.txtfile into the/appdirectory of the container - Entrypoint: Sets the container entrypoint to
cat /app/hello.txt, so the container displays the file content when run - Configuration: Sets working directory to
/app, adds labels and environment variables - Outputs: Creates both an OCI image and a tarball for distribution
For a quick demonstration, run the demo script:
./demo.shThis will build the container and show you how to test it with Docker.
bazel build //:dhi_alpine_app --platforms=//platforms:linux_amd64bazel build //:dhi_alpine_app_tarball --platforms=//platforms:linux_amd64The tarball can be found at bazel-bin/dhi_alpine_app_tarball/tarball.tar and can be loaded into Docker:
docker load < bazel-bin/dhi_alpine_app_tarball/tarball.tarAfter loading the tarball into Docker:
# Run the container (will automatically display hello.txt content due to entrypoint)
docker run --rm dhi-alpine-app:latest
# You can also run other commands by overriding the entrypoint
docker run --rm --entrypoint="" dhi-alpine-app:latest ls -la /app:app_layer- Creates a tar layer containing the hello.txt file:dhi_alpine_app- The main OCI image target:dhi_alpine_app_tarball- Creates a tarball of the image for easy distribution
rules_oci@1.7.6- OCI container image building rulesrules_pkg@1.0.1- Package creation rulesrules_cc@0.0.9- C/C++ rules (needed for platform definitions)platforms@0.0.10- Platform definitions
The DHI Alpine base 3.22 image is pulled from the DHI registry:
oci.pull(
name = "alpine",
image = "index.docker.io/dhi/alpine-base:3.22",
platforms = ["linux/amd64"],
)- Working Directory:
/app - Entrypoint:
cat /app/hello.txt- automatically displays the hello.txt content when container runs - Environment Variables:
APP_NAME=alpine_exampleAPP_VERSION=1.0.0
- Labels: Includes standard OCI image labels
- File Permissions: Copied file has mode
0644
To adapt this example for your needs:
- Change the base image: Modify the
oci.pullblock inMODULE.bazel - Add more files: Update the
srcslist in thepkg_tartarget - Modify container config: Update labels, environment variables, or working directory in the
oci_imagetarget - Add build steps: Create additional targets for compiling code before packaging
If you encounter platform-related errors, ensure you're building with the correct platform:
bazel build //:dhi_alpine_app_tarball --platforms=//platforms:linux_amd64The build may show warnings about newer versions of dependencies being available. These can usually be ignored or you can update the versions in MODULE.bazel.
To build for multiple platforms, modify the platforms list in MODULE.bazel and create additional platform definitions.
For more complex applications, consider creating multiple tar layers to optimize container build caching.
Replace the Alpine base with your preferred base image by updating the oci.pull configuration.
This example provides a foundation for building container images with Bazel and can be extended to build more complex applications.
The project includes a comprehensive .gitignore file that excludes:
- Bazel build outputs (
bazel-*symlinks) - Generated lock files (
MODULE.bazel.lock) - IDE files and system files
- Container tarballs (optional)
To initialize a git repository:
git init
git add .
git commit -m "Initial commit: Bazel OCI container example with DHI Alpine base"