You get the idea 🙂 it's the same as the CLI we all know and love.
>>> from python_on_whales import docker
>>> output = docker.run("hello-world")
>>> print(output)
Hello from Docker!
This message shows that your installation appears to be working correctly.
...
>>> from python_on_whales import docker
>>> print(docker.run("nvidia/cuda:11.0-base", ["nvidia-smi"], gpus="all"))
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 450.51.06 Driver Version: 450.51.06 CUDA Version: 11.0 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|===============================+======================+======================|
| 0 Tesla T4 On | 00000000:00:1E.0 Off | 0 |
| N/A 34C P8 9W / 70W | 0MiB / 15109MiB | 0% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=============================================================================|
| No running processes found |
+-----------------------------------------------------------------------------+
from python_on_whales import docker
# since it's interactive, you'll be dropped into the psql shell. The python code
# will continue only after you exit the shell.
docker.run(
"postgres:9.6",
["psql", "-h", "some-postgres", "-U", "postgres"],
networks=["some-network"],
interactive=True,
tty=True,
remove=True,
)
Any Docker object can be used as a context manager to ensure it's removed even if an exception occurs:
from python_on_whales import docker
with docker.volume.create("random_name") as some_volume:
docker.run(
"postgres:9.6",
["-c", "shared_buffers=256MB", "-c", "max_connections=200"],
name="some-postgres",
envs={"POSTGRES_PASSWORD": "mysecretpassword", "PGDATA": "/var/lib/postgresql/data/pgdata"},
volumes=[(some_volume, "/var/lib/postgresql/data"), ("myvolume", "/tmp/myvolume")],
detach=True,
)
# so some stuff here
# here we are out of the context manager, so the volume has been removed, even if there was an exception.
Main features
1 to 1 mapping between the CLI interface and the Python API. No need to look in the docs
what is the name of the function/argument you need.
Support for the latest Docker features:
Docker buildx/buildkit,
docker run --gpu=all ...
Support for Docker stack, services and Swarm (same API as the command line).
Progress bars and progressive outputs when pulling, pushing, loading, building...
Support for some other CLI commands that are not in Docker-py:
docker cp, docker run --cpus ... and more.
Nice SSH support for remote daemons.
Docker object as Python objects: Container, Images, Volumes, Services... and their
attributes are updated in real-time!
Each Docker object can be used as a context manager. When getting out of the
context, the Docker object is removed automatically, even if an exception occurs.
A fully typed API (Mypy and IDE-friendly) compatible with pathlib and os.path
All Docker objects and the Docker client are safe to use with multithreading and multiprocessing.
Display the commands called and the environment variables used by setting the environment variable PYTHON_ON_WHALES_DEBUG=1.
Why another project? Why not build on Docker-py?
In a sense this project is built on top of Docker-py
because the implementation, the organisation and the API is inspired from the project, but the codebases
could not be the same.
Two major differences do not permit that:
The API is quite different. The aim of Python on Whales is to provide a 1-to-1
mapping between the Docker command line and Python, so that users don't even have
to open the docs to do write code.
While Docker-py is a complete re-implementation of the Docker client binary
(written in Go), Python on whales sits on top of the Docker client binary, which makes
implementing new features much easier and safer. For example, it's
unlikely that docker-py supports Buildx/buildkit
anytime soon because rewriting a large Go codebase in Python is hard work.
Should I use Docker-py or Python on Whales?
Well, it's written in each project's description!
Docker-py: A Python library for the Docker Engine API
Python on whales: An awesome Python wrapper for an awesome Docker CLI
If you need to talk to the Docker engine directly, you need to do low level operations,
use docker-py. Some good example would be writing the code to control docker from an IDE,
or if the speed of Docker calls is very important.
If you don't want to depend on the Docker CLI binary (~50MB), use docker-py.
If you wanted to call the docker command line from Python,
do high level operations, use Python on Whales.
For example if you want to write your CI logic in Python rather than in bash (a very good choice 😉).
Some commands are only available in Python on whales
too: docker.buildx.build(...), docker.stack.deploy(...)...
Use the right tool for the right job 🙂
Alternatives to Docker: Podman, nerdctl...
Support for Docker-compatible clients like Podman and Nerdctl was introduced in Python-on-whales version 0.44.0.
You can use an arbitrary binary to execute Docker commands by using the argument client_call of python_on_whales.DockerCLient.
Here is an example: