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:
- Docker Buildx: With
docker-py
, you can use theClient.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
)
- Docker Compose: With
docker-py
, you can use theClient.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
)
- Docker Swarm: With
docker-py
, you can use theClient.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:
- Guest Post: Calling the Docker CLI from Python with Python-on-whales | Docker
- Docker Engine API SDKs | Docker Docs
- Examples using the Docker Engine SDKs and Docker API | Docker Docs
- CI/CD: Flask App + GitHub, Travis, & Heroku – Sweetcode.io
- Containerizing Test Tooling: Creating your Dockerfile and Makefile | Docker
- Running a Python application on Kubernetes | Opensource.com
- Build secure container images with RHEL UBI | Red Hat Developer
- How to “Dockerize” Your Python Applications | Docker
- [Resources to Use Javascript, Python, Java, and Go with Docker | Docker](https://www.docker.