Kyle Galbraith
Posted on February 3, 2023
The ability to build multiple Docker images from a single file is now available in our latest depot
CLI version. With the depot bake
command, you can now build multiple images from a single HCL, JSON, or Docker Compose file. It's a fantastic way to build all the images related to your application with a single build request to Depot.
What is bake
?
In a nutshell, bake
is a high-level build command that allows you to build multiple images from a single file via BuildKit. The bake
command allows you to issue a single build request with the file containing the image definitions to be built. All of the images get built concurrently against a single BuildKit builder.
With Depot, we are orchestrating BuildKit builders for every build you route to us. So a builder and its persistent cache are spawned for each architecture you request in a build. But a single Depot builder (i.e., a project) can handle multiple builds concurrently.
Build multiple images concurrently
Before bake
was supported, you would often need to run multiple depot build
commands to build multiple images with a single builder. A lot of our users have reached for a Makefile
to do this:
build:
depot build --project 1234567890 -f Dockerfile.app
depot build --project 1234567890 -f Dockerfile.db
depot build --project 1234567890 -f Dockerfile.cron
But this results in the images building serially. First, build the app image, then the DB, and finally, the cron image. It works, but you have to issue a separate build request for each image you want to build.
With bake
, you can build all these images concurrently with a single build request. To do it, you define a file that contains how all the images should be built; it can be written in an HCL, JSON, or Docker Compose file. Here is an example of a docker-bake.hcl
file that produces the same images as the Makefile
above:
group "default" {
targets = ["original", "db", "cron"]
}
target "app" {
dockerfile = "Dockerfile.app"
platforms = ["linux/amd64", "linux/arm64"]
tags = ["repo/app:test"]
}
target "db" {
dockerfile = "Dockerfile.db"
platforms = ["linux/amd64", "linux/arm64"]
tags = ["repo/db:test"]
}
target "cron" {
dockerfile = "Dockerfile.cron"
platforms = ["linux/amd64", "linux/arm64"]
tags = ["repo/cron:test"]
}
The entire build definition from the Makefile
above is now contained in a single file. Bake files can specify all of the parameters you would pass to an image build, notice how the file above is using the platforms
and tags
parameters, much like --platform
and --tag
when running depot build
.
With this file, you can now run depot bake
to build all the images in the file:
depot bake --project 1234567890 -f docker-bake.hcl
What you see in the output is multiple images being built concurrently. So, for example, you can see that the app
image is being made for both linux/amd64
and linux/arm64
at the same time as the db
and cron
images are being built for both architectures.
=> [cron linux/arm64 2/3] WORKDIR /app 11.7s
=> [cron linux/amd64 2/3] WORKDIR /app 12.5s
=> [app linux/arm64 build 2/6] WORKDIR /app 6.2s
=> [app linux/amd64 build 2/6] WORKDIR /app 6.6s
=> [app linux/arm64 build 3/6] COPY package.json yarn.lock tsconfig.json ./ 0.0s
=> [db linux/arm64 3/3] RUN echo "postgres build" 0.2s
=> [app linux/arm64 build 4/6] COPY src/ ./src/ 0.0s
=> [app linux/arm64 build 5/6] RUN yarn install --immutable 9.3s
=> [app linux/amd64 build 3/6] COPY package.json yarn.lock tsconfig.json ./ 0.1s
=> [db linux/amd64 3/3] RUN echo "postgres build" 0.2s
=> [app linux/amd64 build 4/6] COPY src/ ./src/ 0.0s
=> [app linux/amd64 build 5/6] RUN yarn install --immutable 9.5s
=> [app linux/arm64 build 6/6] RUN yarn build 4.2s
=> [app linux/amd64 build 6/6] RUN yarn build 4.4s
=> [app linux/arm64 stage-1 3/5] COPY --from=build /app/node_modules /app/node_modules 1.6s
depot/bake-action
GitHub Action
In addition to the depot bake
command, we released depot/bake-action
GitHub Action that implements the same inputs and outputs as the docker/bake-action
.
Like our depot/build-push-action
, you can use GitHub's OpenID Connect tokens via a trust relationship, so your builds can authenticate with Depot projects without needing any static access tokens. Here is an example GitHub Action workflow that builds images from a docker-bake.hcl
file:
name: Run Depot bake
on: push
permissions:
id-token: write
contents: read
jobs:
build:
name: Build
runs-on: ubuntu-20.04
steps:
- name: Checkout repo
uses: actions/checkout@v3
- uses: depot/setup-action@v1
- name: Bake Docker images
uses: depot/bake-action@v1
with:
project: 1234567890
file: docker-bake.hcl
Conclusion
Building multiple images via depot bake
is a great way to speed up your image builds even more. With bake
, you can leverage the ability of BuildKit to build multiple images concurrently and deduplicate work across multiple images specified in a single build request.
Define all the images that compose your application in a single HCL, JSON, or Docker Compose file and run depot bake
to build them all at once. No more waiting for each image to build one at a time. Instead, you can build them all concurrently on the same builder.
For those of you leveraging GitHub Actions, you can use the depot/bake-action
to build images from your file inside your existing Actions workflows. Just swap in depot/bake-action
for docker/bake-action
, and you're good to go.
If you're not using Depot, we offer a 14-day free trial to let you try things out in your existing workflows to see if we can make your image builds faster. We are the only remote container build service that offers native multi-platform image support, and we make caching a breeze because it's automatically persisted for you across builds. These two features usually make image builds 15x faster in CI environments.
Sign up today for a 14-day free trial and swap docker build
for depot build
to get instantly faster builds: https://depot.dev/sign-up.
Posted on February 3, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 20, 2024
November 19, 2024