| Bazel CI |
|---|
🚨 rules_docker is no longer maintained and deprecated. Please see rules_oci for a better designed and maintained alternative.
These rules used to be docker_build, docker_push, etc. and the aliases for
these (mostly) legacy names still exist largely for backwards-compatibility. We
also have early-stage oci_image, oci_push, etc. aliases for folks that
enjoy the consistency of a consistent rule prefix. The only place the
format-specific names currently do any more than alias things is in foo_push,
where they also specify the appropriate format as which to publish the image.
This repository contains a set of rules for pulling down base images, augmenting them with build artifacts and assets, and publishing those images. These rules do not require / use Docker for pulling, building, or pushing images. This means:
boot2docker or docker-machine installed. Note use of these rules on Windows
is currently not supported.Also, unlike traditional container builds (e.g. Dockerfile), the Docker images
produced by container_image are deterministic / reproducible.
To get started with building Docker images, check out the examples that build the same images using both rules_docker and a Dockerfile.
NOTE: container_push and container_pull make use of
google/go-containerregistry for
registry interactions.
It is notable that: cc_image, go_image, rust_image, and d_image
also allow you to specify an external binary target.
This repo now includes rules that provide additional functionality to install packages and run commands inside docker containers. These rules, however, require a docker binary is present and properly configured. These rules include:
In addition to low-level rules for building containers, this repository
provides a set of higher-level rules for containerizing applications. The idea
behind these rules is to make containerizing an application built via a
lang_binary rule as simple as changing it to lang_image.
By default these higher level rules make use of the distroless language runtimes, but these
can be overridden via the base="..." attribute (e.g. with a container_pull
or container_image target).
Note also that these rules do not expose any docker related attributes. If you
need to add a custom env or symlink to a lang_image, you must use
container_image targets for this purpose. Specifically, you can use as base for your
lang_image target a container_image target that adds e.g., custom env or symlink.
Please see <a href=#go_image-custom-base>go_image (custom base)</a> for an example.
Add the following to your WORKSPACE file to add the external repositories:
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") http_archive( # Get copy paste instructions for the http_archive attributes from the # release notes at https://github.com/bazelbuild/rules_docker/releases ) # OPTIONAL: Call this to override the default docker toolchain configuration. # This call should be placed BEFORE the call to "container_repositories" below # to actually override the default toolchain configuration. # Note this is only required if you actually want to call # docker_toolchain_configure with a custom attr; please read the toolchains # docs in /toolchains/docker/ before blindly adding this to your WORKSPACE. # BEGIN OPTIONAL segment: load("@io_bazel_rules_docker//toolchains/docker:toolchain.bzl", docker_toolchain_configure="toolchain_configure" ) docker_toolchain_configure( name = "docker_config", # OPTIONAL: Bazel target for the build_tar tool, must be compatible with build_tar.py build_tar_target="<enter absolute path (i.e., must start with repo name @...//:...) to an executable build_tar target>", # OPTIONAL: Path to a directory which has a custom docker client config.json. # See https://docs.docker.com/engine/reference/commandline/cli/#configuration-files # for more details. client_config="<enter Bazel label to your docker config.json here>", # OPTIONAL: Path to the docker binary. # Should be set explicitly for remote execution. docker_path="<enter absolute path to the docker binary (in the remote exec env) here>", # OPTIONAL: Path to the gzip binary. gzip_path="<enter absolute path to the gzip binary (in the remote exec env) here>", # OPTIONAL: Bazel target for the gzip tool. gzip_target="<enter absolute path (i.e., must start with repo name @...//:...) to an executable gzip target>", # OPTIONAL: Path to the xz binary. # Should be set explicitly for remote execution. xz_path="<enter absolute path to the xz binary (in the remote exec env) here>", # OPTIONAL: Bazel target for the xz tool. # Either xz_path or xz_target should be set explicitly for remote execution. xz_target="<enter absolute path (i.e., must start with repo name @...//:...) to an executable xz target>", # OPTIONAL: List of additional flags to pass to the docker command. docker_flags = [ "--tls", "--log-level=info", ], ) # End of OPTIONAL segment. load( "@io_bazel_rules_docker//repositories:repositories.bzl", container_repositories = "repositories", ) container_repositories() load("@io_bazel_rules_docker//repositories:deps.bzl", container_deps = "deps") container_deps() load( "@io_bazel_rules_docker//container:container.bzl", "container_pull", ) container_pull( name = "java_base", registry = "gcr.io", repository = "distroless/java", # 'tag' is also supported, but digest is encouraged for reproducibility. digest = "sha256:deadbeef", )
If the repositories that are imported by container_repositories() have already been
imported (at a different version) by other rules you called in your WORKSPACE, which
are placed above the call to container_repositories(), arbitrary errors might
occur. If you get errors related to external repositories, you will likely
not be able to use container_repositories() and will have to import
directly in your WORKSPACE all the required dependencies (see the most up
to date impl of container_repositories() for details).
This is an example of an error due to a diamond dependency. If you get this error, make sure to import rules_docker before other libraries, so that six can be patched properly.
See https://github.com/bazelbuild/rules_docker/issues/1022 for more details.
BUILD or BUILD.bazel file at the top level. This
can be a blank file if necessary. Otherwise you might see an error that looks
like:Unable to load package for //:WORKSPACE: BUILD file not found in any of the following directories.
build --@io_bazel_rules_docker//transitions:enable=false
Suppose you have a container_image target //my/image:helloworld:
container_image( name = "helloworld", ... )
You can load this into your local Docker client by running:
bazel run my/image:helloworld.
For the lang_image targets, this will also run the
container using docker run to maximize compatibility with lang_binary rules.
Arguments to this command are forwarded to docker, meaning the command
bazel run my/image:helloworld -- -p 8080:80 -- arg0
performs the following steps:
my/image:helloworld target into your local Docker clientarg0 is passed to the image entrypointdocker run documentationYou can suppress this behavior by passing the single flag: bazel run :foo -- --norun
Alternatively, you can build a docker load compatible bundle with:
bazel build my/image:helloworld.tar. This will produce a tar file
in your bazel-out directory that can be loaded into your local Docker
client. Building this target can be expensive for large images. You will
first need to query the ouput file location.
TARBALL_LOCATION=$(bazel cquery my/image:helloworld.tar \ --output starlark \ --starlark:expr="target.files.to_list()[0].path") docker load -i $TARBALL_LOCATION
These work with both container_image, container_bundle, and the
lang_image rules. For everything except container_bundle, the image
name will be bazel/my/image:helloworld. The container_bundle rule will
apply the tags you have specified.
You can use these rules to access private images using standard Docker authentication methods. e.g. to utilize the Google Container Registry. See here for authentication methods.
See also:
Once you've setup your docker client configuration, see here
for an example of how to use container_pull with custom docker authentication credentials
and here for an example of how
to use container_push with custom docker authentication credentials.
A common request from folks using
container_push, container_bundle, or container_image is to
be able to vary the tag that is pushed or embedded. There are two options
at present for doing this.
The first option is to use stamping.
Stamping is enabled when bazel is run with --stamp.
This enables replacements in stamp-aware attributes.
A python format placeholder (e.g. {BUILD_USER})
is replaced by the value of the corresponding workspace-status variable.
# A common pattern when users want to avoid trampling # on each other's images during development. container_push( name = "publish", format = "Docker", # Any of these components may have variables. registry = "gcr.io", repository = "my-project/my-image", # This will be replaced with the current user when built with --stamp tag = "{BUILD_USER}", )
Rules that are sensitive to stamping can also be forced to stamp or non-stamp mode irrespective of the
--stampflag to Bazel. Use thebuild_context_datarule to make a target that providesStampSettingInfo, and pass this to thebuild_context_dataattribute.
The next natural question is: "Well what variables can I use?" This
option consumes the workspace-status variables Bazel defines in
bazel-out/stable-status.txt and bazel-out/volatile-status.txt.
Note that changes to the stable-status file cause a rebuild of the action, while volatile-status does not.
You can add more stamp variables via --workspace_status_command,
see the bazel docs.
A common example is to provide the current git SHA, with
--workspace_status_command="echo STABLE_GIT_SHA $(git rev-parse HEAD)"
That flag is typically passed in the .bazelrc file, see for example .bazelrc in kubernetes.
The second option is to employ Makefile-style variables:
container_bundle( name = "bundle", images = { "gcr.io/$(project)/frontend:latest": "//frontend:image", "gcr.io/$(project)/backend:latest": "//backend:image", } )
These variables are specified on the CLI using:
bazel build --define project=blah //path/to:bundle
lang_image rulesBy default the lang_image rules use the distroless base runtime images,
which are optimized to be the minimal set of things your application needs
at runtime. That can make debugging these containers difficult because they
lack even a basic shell for exploring the filesystem.
To address this, we publish variants of the distroless runtime images tagged
:debug, which are the exact-same images, but with additions such as busybox
to make debugging easier.
For example (in this repo):
$ bazel run -c dbg testdata:go_image ... INFO: Build completed successfully, 5 total actions INFO: Running command line: bazel-bin/testdata/go_image Loaded image ID: sha256:9c5c2167a1db080a64b5b401b43b3c5cdabb265b26cf7a60aabe04a20da79e24 Tagging 9c5c2167a1db080a64b5b401b43b3c5cdabb265b26cf7a60aabe04a20da79e24 as bazel/testdata:go_image Hello, world! $ docker run -ti --rm --entrypoint=sh bazel/testdata:go_image -c "echo Hello, busybox." Hello, busybox.
container_image( name = "app", # References container_pull from WORKSPACE (above) base = "@java_base//image", files = ["//java/com/example/app:Hello_deploy.jar"], cmd = ["Hello_deploy.jar"] )
Hint: if you want to put files in specific directories inside the image
use <a href="https://docs.bazel.build/versions/master/be/pkg.html">pkg_tar rule</a>
to create the desired directory structure and pass that to container_image via
tars attribute. Note you might need to set strip_prefix = "." or strip_prefix = "{some directory}"
in your rule for the files to not be flattened.
See <a href="https://github.com/bazelbuild/bazel/issues/2176">Bazel upstream issue 2176</a> and
<a href="https://github.com/bazelbuild/rules_docker/issues/317">rules_docker issue 317</a>
for more details.
To use cc_image, add the following to WORKSPACE:
load( "@io_bazel_rules_docker//repositories:repositories.bzl", container_repositories = "repositories", ) container_repositories() load(


全球首个AI音乐社区
音述AI是全球首个AI音乐社区,致力让每个人都能用音乐表达自我。音述AI提供零门槛AI创作工具,独创GETI法则帮助用户精准定义音乐风格,AI润色功能支持自动优化作品质感。音述AI支持交流讨论、二次创作与价值变现。针对中文用户的语言习惯与文化背景进行专门优化,支持国风融合、C-pop等本土音乐标签,让技术更好地承载人文表达。


阿里Qoder团队推出的桌面端AI智能体
QoderWork 是阿里推出的本地优先桌面 AI 智能体,适配 macOS14+/Windows10+,以自然语言交互实现文件管理、数据分析、AI 视觉生成、浏览器自动化等办公任务,自主拆解执行复杂工作流,数据本地运行零上传,技能市场可无限扩展,是高效的 Agentic 生产力办公助手。


一站式搞定所有学习需求
不再被海量信息淹没,开始真正理解知识。Lynote 可摘要 YouTube 视频、PDF、文章等内容。即时创建笔记,检测 AI 内容并下载资料,将您的学习效率提升 10 倍。


为AI短剧协作而生
专为AI短剧协作而生的AniShort正式发布,深度重构AI短剧全流程生产模式,整合创意策划、制作 执行、实时协作、在线审片、资产复用等全链路功能,独创无限画布、双轨并行工业化工作流与Ani智能体助手,集成多款主流AI大模型,破解素材零散、版本混乱、沟通低效等行业痛点,助力3人团队效率提升800%,打造标准化、可追溯的AI短剧量产体系,是AI短剧团队协同创作、提升制作效率的核心工具。


能听懂你表达的视频模型
Seedance two是基于seedance2.0的中国大模型,支持图像、视频、音频、文本四种模态输入,表达方式更丰富,生成也更可控。


国内直接访问,限时3折
输入简单文字,生成想要的图片,纳米香蕉中文站基于 Google 模型的 AI 图片生成网站,支持文字生图、图生图。官网价格限时3折活动


职场AI,就用扣子
AI办公助手,复杂任务高效处理。办公效率低?扣子空间AI助手支持播客生成、PPT制作、网页开发及报告写作,覆盖科研、商业、舆情等领域的专家Agent 7x24小时响应,生活工作无缝切换,提升50%效率!


多风格AI绘画神器
堆友平台由阿里巴巴设计团队创建,作为一款AI驱动的设计工具,专为设计师提供一站式增长服务。功能覆盖海量3D素材、AI绘 画、实时渲染以及专业抠图,显著提升设计品质和效率。平台不仅提供工具,还是一个促进创意交流和个人发展的空间,界面友好,适合所有级别的设计师和创意工作者。


零代码AI应用开发平台
零代码AI应用开发平台,用户只需一句话简单描述需求,AI能自动生成小程序、APP或H5网页应用,无需编写代码。


免费创建高清无水印Sora视频
Vora是一个免费创建高清无水印Sora视频的AI工具
最新AI工具、AI资讯
独家AI资源、AI项目落地

微信扫一扫关注公众号