Integration with Other Tools - docker/docker-py

The docker-py library is a Python client for the Docker Engine API, which allows you to interact with Docker from Python. However, it is not always a 1-to-1 mapping with the Docker CLI, and some features may not be available.

An alternative to docker-py is python-on-whales, which provides a 1-to-1 mapping between the Docker CLI and the Python library by communicating with the Docker CLI instead of calling directly the Docker Engine HTTP API.

Here are some examples of how docker-py and python-on-whales can be used to interact with other Docker tools and platforms:

  1. Docker Buildx: With docker-py, you can use the Client.images.build method to build images using Docker Buildx. Here’s an example:
import docker

client = docker.from_env()
response = client.images.build(
path="./my-app",
dockerfile="Dockerfile",
rm=True,
network_mode="host",
buildargs={"VAR1":"value1", "VAR2":"value2"},
tags=["my-app:latest"],
forcerm=True,
pull=True,
quiet=False,
tag_until_push=False,
cache_from=None,
cache_to=None,
labels=None,
shm_size=None,
extra_hosts=None,
session=None,
decode=True,
stream=False,
socket=None
)

With python-on-whales, you can use the docker.build method to build images using Docker Buildx. Here’s an example:

import python_on_whales as podman

response = podman.build(
path="./my-app",
dockerfile="Dockerfile",
rm=True,
network_mode="host",
buildargs={"VAR1":"value1", "VAR2":"value2"},
tags=["my-app:latest"],
forcerm=True,
pull=True,
quiet=False,
tag_until_push=False,
cache_from=None,
cache_to=None,
labels=None,
shm_size=None,
extra_hosts=None,
stream=False
)
  1. Docker Compose: With docker-py, you can use the Client.compose method to interact with Docker Compose. Here’s an example:
import docker

client = docker.from_env()
response = client.compose.build(
path="./my-app",
dockerfile="Dockerfile",
rm=True,
network_mode="host",
buildargs={"VAR1":"value1", "VAR2":"value2"},
tags=["my-app:latest"],
forcerm=True,
pull=True,
quiet=False,
tag_until_push=False,
cache_from=None,
cache_to=None,
labels=None,
shm_size=None,
extra_hosts=None,
stream=False,
project_name="my-app",
project_directory="./my-app",
no_build=False,
use_lemmatization=True,
volumes=None,
cap_add=None,
cap_drop=None,
dns=None,
dns_opt=None,
)

With python-on-whales, you can use the docker.compose method to interact with Docker Compose. Here’s an example:

import python_on_whales as podman

response = podman.compose(
build=True,
path="./my-app",
dockerfile="Dockerfile",
rm=True,
network_mode="host",
buildargs={"VAR1":"value1", "VAR2":"value2"},
tags=["my-app:latest"],
forcerm=True,
pull=True,
quiet=False,
tag_until_push=False,
cache_from=None,
cache_to=None,
labels=None,
shm_size=None,
extra_hosts=None,
stream=False,
project_name="my-app",
project_directory="./my-app",
no_build=False,
use_lemmatization=True,
volumes=None,
cap_add=None,
cap_drop=None,
dns=None,
dns_opt=None
)
  1. Docker Swarm: With docker-py, you can use the Client.swarm method to interact with Docker Swarm. Here’s an example:
import docker

client = docker.from_env()
response = client.swarm.init(
advertise_addr="192.168.99.100:2377",
listen_addr="0.0.0.0:2377",
force_new_cluster=False,
spec=None,
secret_key=None,
ca_cert=None,
ca_key=None,
cert_file=None,
key_file=None,
skip_node_validation=False,
data_path_addr="unix:///var/run/docker/swarm/promote.sock"
)

With python-on-whales, you can use the docker.swarm method to interact with Docker Swarm. Here’s an example:

import python_on_whales as podman

response = podman.swarm.init(
advertise_addr="192.168.99.100:2377",
listen_addr="0.0.0.0:2377",
force_new_cluster=False,
spec=None,
secret_key=None,
ca_cert=None,
ca_key=None,
cert_file=None,
key_file=None,
skip_node_validation=False,
data_path_addr="unix:///var/run/docker/swarm/promote.sock"
)

Sources: