docker buildx
BuildxCLI
bake
bake(targets=[], builder=None, files=[], load=False, cache=True, print=False, progress='auto', pull=False, push=False, set={}, variables={}, stream_logs=False)
Bake is similar to make, it allows you to build things declared in a file.
For example it allows you to build multiple docker image in parallel.
The CLI docs is here and it contains a lot more information.
PARAMETER | DESCRIPTION |
---|---|
targets |
Targets or groups of targets to build.
TYPE:
|
builder |
The builder to use.
TYPE:
|
files |
Build definition file(s)
TYPE:
|
load |
Shorthand for
TYPE:
|
cache |
Whether to use the cache or not.
TYPE:
|
print |
Do nothing, just returns the config.
TYPE:
|
progress |
Set type of progress output (
TYPE:
|
pull |
Always try to pull the newer version of the image
TYPE:
|
push |
Shorthand for
TYPE:
|
set |
A list of overrides in the form
TYPE:
|
variables |
A dict containing the values of the variables defined in the hcl file. See https://github.com/docker/buildx#hcl-variables-and-functions
TYPE:
|
Returns
The configuration used for the bake (files merged + override with
the arguments used in the function). It's the loaded json you would
obtain by running `docker buildx bake --print --load my_target` if
your command was `docker buildx bake --load my_target`. Some example here.
from python_on_whales import docker
# returns the config used and runs the builds
config = docker.buildx.bake(["my_target1", "my_target2"], load=True)
assert config == {
"target": {
"my_target1": {
"context": "./",
"dockerfile": "Dockerfile",
"tags": ["pretty_image1:1.0.0"],
"target": "out1",
"output": ["type=docker"]
},
"my_target2": {
"context": "./",
"dockerfile": "Dockerfile",
"tags": ["pretty_image2:1.0.0"],
"target": "out2",
"output": ["type=docker"]
}
}
}
# returns the config only, doesn't run the builds
config = docker.buildx.bake(["my_target1", "my_target2"], load=True, print=True)
build
build(context_path, add_hosts={}, allow=[], attest=None, build_args={}, build_contexts={}, builder=None, cache=True, cache_from=None, cache_to=None, file=None, labels={}, load=False, network=None, output={}, platforms=None, progress='auto', provenance=None, pull=False, push=False, sbom=None, secrets=[], ssh=None, tags=[], target=None, stream_logs=False)
Build a Docker image with builkit as backend.
Alias: docker.build(...)
A python_on_whales.Image
is returned, even when using multiple tags.
That is because it will produce a single image with multiple tags.
If no image is loaded into the Docker daemon (if push=True
for ex),
then None
is returned.
PARAMETER | DESCRIPTION |
---|---|
context_path |
The path of the build context.
TYPE:
|
add_hosts |
Hosts to add.
TYPE:
|
allow |
List of extra privileges.
Eg
TYPE:
|
attest |
Attestation parameters. Eg
TYPE:
|
build_args |
The build arguments.
ex
TYPE:
|
build_contexts |
Additional build contexts.
TYPE:
|
builder |
Specify which builder to use.
TYPE:
|
cache |
Whether or not to use the cache
TYPE:
|
cache_from |
Works only with the container driver. Loads the cache
(if needed) from a registry
TYPE:
|
cache_to |
Works only with the container driver. Sends the resulting
docker cache either to a registry
TYPE:
|
file |
The path of the Dockerfile
TYPE:
|
labels |
Dict of labels to add to the image.
TYPE:
|
load |
Shortcut for
TYPE:
|
network |
which network to use when building the Docker image
TYPE:
|
output |
Output destination
(format:
TYPE:
|
platforms |
List of target platforms when building the image. Ex:
TYPE:
|
progress |
Set type of progress output (auto, plain, tty, or False). Use plain to keep the container output on screen
TYPE:
|
provenance |
Shortand for
TYPE:
|
pull |
Always attempt to pull a newer version of the image
TYPE:
|
push |
Shorthand for
TYPE:
|
sbom |
Shorthand for
TYPE:
|
secrets |
One or more secrets passed as string(s). For example
TYPE:
|
ssh |
SSH agent socket or keys to expose to the build
(format is
TYPE:
|
tags |
Tag or tags to put on the resulting image.
TYPE:
|
target |
Set the target build stage to build.
TYPE:
|
stream_logs |
If
TYPE:
|
Returns
A `python_on_whales.Image` if a Docker image is loaded
in the daemon after the build (the default behavior when
calling `docker.build(...)`). Otherwise, `None`.
create
create(context_or_endpoint=None, bootstrap=False, buildkitd_flags=None, config=None, platforms=None, driver=None, driver_options={}, name=None, use=False)
Create a new builder instance
PARAMETER | DESCRIPTION |
---|---|
context_or_endpoint |
TYPE:
|
bootstrap |
Boot builder after creation
TYPE:
|
buildkitd_flags |
Flags for buildkitd daemon
TYPE:
|
config |
BuildKit config file
TYPE:
|
platforms |
Comma-separated list of platforms of the form OS/architecture/variant. Ex:
TYPE:
|
driver |
Driver to use (available: [kubernetes docker docker-container])
TYPE:
|
driver_options |
Options for the driver.
e.g
TYPE:
|
name |
Builder instance name
TYPE:
|
use |
Set the current builder instance to this builder
TYPE:
|
Returns
A `python_on_whales.Builder` object.
disk_usage
disk_usage()
Not yet implemented
inspect
inspect(x=None, bootstrap=False)
Returns a builder instance from the name.
PARAMETER | DESCRIPTION |
---|---|
x |
If
TYPE:
|
bootstrap |
If set to True, ensure builder has booted before inspecting.
TYPE:
|
Returns
A `python_on_whales.Builder` object.
is_installed
is_installed()
Returns True
if docker buildx is installed and working.
If it's not installed, head to the installation page and follow the instructions.
list
list()
Returns the list of python_on_whales.Builder
available.
prune
prune(all=False, filters={}, stream_logs=False)
Remove build cache on the current builder.
PARAMETER | DESCRIPTION |
---|---|
all |
Remove all cache, not just dangling layers
TYPE:
|
filters |
Filters to use, for example
TYPE:
|
stream_logs |
If
TYPE:
|
remove
remove(builder)
Remove a builder
PARAMETER | DESCRIPTION |
---|---|
builder |
The builder to remove
TYPE:
|
stop
stop(builder)
Stop the builder instance
PARAMETER | DESCRIPTION |
---|---|
builder |
The builder to stop. If
TYPE:
|
use
use(builder, default=False, global_=False)
Set the current builder instance
PARAMETER | DESCRIPTION |
---|---|
builder |
The builder to use
TYPE:
|
default |
Set builder as default for the current context
TYPE:
|
global_ |
Builder will be used even when changing contexts
TYPE:
|
version
version()
Returns the docker buildx version as a string.
from python_on_whales import docker
version = docker.buildx.version()
print(version)
# "github.com/docker/buildx v0.4.2 fb7b670b764764dc4716df3eba07ffdae4cc47b2"
ImagetoolsCLI
create
create(sources=[], tags=[], append=False, files=[], dry_run=False, builder=None)
Create a new manifest list based on source manifests. The source manifests can be manifest lists or single platform distribution manifests and must already exist in the registry where the new manifest is created. If only one source is specified, create performs a carbon copy.
The CLI docs is here and it contains a lot more information.
PARAMETER | DESCRIPTION |
---|---|
sources |
The sources manifest to create, change
TYPE:
|
append |
Append to existing manifest
TYPE:
|
dry_run |
Show final image instead of pushing
TYPE:
|
files |
Read source descriptor from file
TYPE:
|
builder |
The builder to use.
TYPE:
|
inspect
inspect(name)
Returns the manifest of a Docker image in a registry without pulling it
Notes about the transition between the legacy builder and buildx
Users are encouraged to use buildx in Python-on-whales through the
docker.build()
function.
Buildx is the next gen Docker builder and a transition is underway to make
the docker build
shell command use buildx. Python-on-whales has had an opinionated
answer on the matter as docker.build()
will always use buildx. This is because Python-on-whales was created
during the transition and doesn't have an existing user codebase to support.
The legacy builder is still available by calling docker.legacy_build()
, but note that
- It won't work if you use Docker 22.06 or above
- It won't work if you used
docker.buildx.install()
ordocker buildx install
previously - It won't work if you had set the environment variable
DOCKER_BUILDKIT
to1
Some resources on the matter: