docker image
ImageCLI
exists
exists(x)
Returns True
if the image exists. False
otherwise.
It's just calling docker.image.inspect(...)
and verifies that it doesn't throw
a python_on_whales.exceptions.NoSuchImage
.
Returns
A `bool`
history
history()
Not yet implemented
import_
import_(source, tag=None, changes=(), message=None, platform=None)
Import the contents from a tarball to create a filesystem image
Alias: docker.import_(...)
PARAMETER | DESCRIPTION |
---|---|
changes |
Apply Dockerfile instruction to the created image
TYPE:
|
message |
Set commit message for imported image
TYPE:
|
platform |
Set platform if server is multi-platform capable
TYPE:
|
inspect
inspect(x)
Creates a python_on_whales.Image
object.
Returns
`python_on_whales.Image`, or `List[python_on_whales.Image]` if the input
was a list of strings.
Raises
`python_on_whales.exceptions.NoSuchImage` if one of the images does not exists.
legacy_build
legacy_build(context_path, add_hosts={}, build_args={}, cache=True, file=None, labels={}, network=None, pull=False, tags=(), target=None)
Build a Docker image with the old Docker builder (meaning not using buildx/buildkit)
As the name implies this is a legacy building method. Users are strongly encouraged to use
docker.build()
instead. The legacy builder will not be available in docker v22.06 and above.
This function also won't run the legacy builder if the environment variable
DOCKER_BUILDKIT
is set to 1
or if you had run previously docker buildx install
from bash
or docker.buildx.install()
from Python.
Some resources on why moving to buildx/buildkit is necessary:
- Proposal: make BuildKit the default builder on Linux
- Deprecated Engine Features: Legacy builder for Linux images
A python_on_whales.Image
is returned, even when using multiple tags.
That is because it will produce a single image with multiple tags.
PARAMETER | DESCRIPTION |
---|---|
context_path |
The path of the build context. Defaults to the current working directory
TYPE:
|
add_hosts |
Hosts to add.
TYPE:
|
build_args |
The build arguments.
ex
TYPE:
|
cache |
Whether or not to use the cache, defaults to True
TYPE:
|
file |
The path of the Dockerfile, defaults to
TYPE:
|
labels |
Mapping of labels to add to the image.
TYPE:
|
network |
which network to use when building the Docker image
TYPE:
|
pull |
Always attempt to pull a newer version of the image
TYPE:
|
tags |
Tag or tags to put on the resulting image.
TYPE:
|
target |
Set the target build stage to build.
TYPE:
|
Returns
A `python_on_whales.Image`
list
list(repository_or_tag=None, filters={}, all=False)
Returns the list of Docker images present on the machine.
Alias: docker.images()
Note that each image may have multiple tags.
Returns
A `List[python_on_whales.Image]` object.
load
load(input, quiet=False)
Loads one or multiple Docker image(s) from a tar or an iterator of bytes
.
Alias: docker.load(...)
PARAMETER | DESCRIPTION |
---|---|
input |
Path or input stream to load the images from.
TYPE:
|
quiet |
If you don't want to display the progress bars.
TYPE:
|
Returns
`None` when using bytes as input. A `List[str]` (list of tags)
when a path is provided.
prune
prune(all=False, filter={})
Remove unused images
PARAMETER | DESCRIPTION |
---|---|
all |
Remove all unused images, not just dangling ones
TYPE:
|
filter |
Provide filter values (e.g.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
str
|
The output of the CLI (the layers removed). |
pull
pull(x, quiet=False, platform=None)
Pull one or more docker image(s)
Alias: docker.pull(...)
PARAMETER | DESCRIPTION |
---|---|
x |
The image name(s) . Can be a string or a list of strings. In case of list, multithreading is used to pull the images. The progress bars might look strange as multiple processes are drawing on the terminal at the same time.
TYPE:
|
quiet |
If you don't want to see the progress bars.
TYPE:
|
platform |
If you want to enforce a platform.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[Image, List[Image]]
|
The Docker image loaded ( |
Union[Image, List[Image]]
|
If a list was passed as input, then a |
Union[Image, List[Image]]
|
be returned. |
push
push(x, quiet=False)
Push a tag or a repository to a registry
Alias: docker.push(...)
PARAMETER | DESCRIPTION |
---|---|
x |
Tag(s) or repo(s) to push. Can be a string or an iterable of strings. If it's an iterable, python-on-whales will push all the images with multiple threads. The progress bars might look strange as multiple processes are drawing on the terminal at the same time.
TYPE:
|
quiet |
If you don't want to see the progress bars.
TYPE:
|
Raises
`python_on_whales.exceptions.NoSuchImage` if one of the images does not exist.
remove
remove(x, force=False, prune=True)
Remove one or more docker images.
PARAMETER | DESCRIPTION |
---|---|
x |
Single image or iterable of Docker images to remove. You can use tags or
TYPE:
|
force |
Force removal of the image(s).
TYPE:
|
prune |
Delete untagged parents.
TYPE:
|
Raises
`python_on_whales.exceptions.NoSuchImage` if one of the images does not exist.
save
save(images, output=None)
Save one or more images to a tar archive. Returns a stream if output is None
Alias: docker.save(...)
PARAMETER | DESCRIPTION |
---|---|
images |
Single image or non-empty iterable of images to save.
TYPE:
|
output |
Path of the tar archive to produce. If
TYPE:
|
Returns
`Optional[Iterator[bytes]]`. If output is a path, nothing is returned.
Raises
`python_on_whales.exceptions.NoSuchImage` if one of the images does not exist.
Example
An example of transfer of an image from a local Docker daemon to a remote Docker daemon. We assume that the remote machine has an ssh access:
from python_on_whales import DockerClient
local_docker = DockerClient()
remote_docker = DockerClient(host="ssh://my_user@186.167.32.84")
image_name = "busybox:1"
local_docker.pull(image_name)
bytes_iterator = local_docker.image.save(image_name)
remote_docker.image.load(bytes_iterator)
Of course the best solution is to use a registry to transfer images, but it's a cool example nonetheless.
tag
tag(source_image, new_tag)
Adds a tag to a Docker image.
Alias: docker.tag(...)
PARAMETER | DESCRIPTION |
---|---|
source_image |
The Docker image to tag. You can use a tag to reference it.
TYPE:
|
new_tag |
The tag to add to the Docker image.
TYPE:
|
Raises
`python_on_whales.exceptions.NoSuchImage` if the image does not exist.