Skip to main content

PyWhales


Run tests PyPI Code style: Ruff Types: ty License: MIT


The Docker and its composer client for Python, designed to be fun and intuitive!

Works on Linux, macOS and Windows, for Python 3.14.8 and above.


How to install?

uv pip install pywhales

See the Docker CLI guide for instructions on installing Docker.

Some cool examples

Start by doing

from pywhales import docker

and then:

  • docker run hello-world -> docker.run("hello-world")
  • docker pull ubuntu -> docker.pull("ubuntu")
  • docker build ./ -> docker.build("./")
  • docker compose up my_service -> docker.compose.up(["my_service"])
  • docker image ls -> docker.image.list()
  • docker ps -> docker.ps()
  • docker cp -> docker.copy()
>>> from pywhales import docker

>>> output = docker.run("hello-world")
>>> print(output)

Hello from Docker!
This message shows that your installation appears to be working correctly.

...
>>> from pywhales import docker
>>> my_image = docker.build(".", tags="some_name")  # uses Buildx/buildkit by default
[+] Building 1.6s (17/17) FINISHED
...

Some more docker.run() advanced examples with postgres

docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres

becomes

from pywhales import docker

docker.run(
    "postgres:9.6",
    name="some-postgres",
    envs={"POSTGRES_PASSWORD": "mysecretpassword"},
    detach=True,
)
print(docker.ps())
# [pywhales.Container(id='f5fb939c409d', name='some-postgres')]

docker run -it --rm --network some-network postgres psql -h some-postgres -U postgres

becomes

from pywhales 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,
)

docker run -d --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -e PGDATA=/var/lib/postgresql/data/pgdata -v /custom/mount:/var/lib/postgresql/data -v myvolume:/tmp/myvolume postgres -c shared_buffers=256MB -c max_connections=200

becomes

from pywhales import docker

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=[
        ("/custom/mount", "/var/lib/postgresql/data"),
        ("myvolume", "/tmp/myvolume"),
    ],
    detach=True,
)

Any Docker object can be used as a context manager to ensure it's removed even if an exception occurs:

from pywhales 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 (ty and IDE-friendly) compatible with pathlib and os.path
  • All Docker objects and the Docker client are safe to use with multithreading and multiprocessing.
  • Enable execution diagnostics by setting PYDOCKER_DEBUG=1. Diagnostics omit command arguments, environment values, and captured output by default. Use DockerClient(raw_diagnostics=True) only when detailed diagnostics are appropriate for that client's logs. The legacy PYTHON_ON_WHALES_DEBUG variable is a fallback only when PYDOCKER_DEBUG is unset. Setting PYDOCKER_DEBUG=0 or leaving its value explicitly empty disables debug output even if the legacy variable is set.

What about the license?

It's a MIT license, so quite permissive.

The license can be found in the git repository.

Metadata

Release files for pywhales 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pywhales 0.5.0
File Size Uploaded Provenance Actions
pywhales-0.5.0.tar.gz 257.7 kB Without provenance Details for pywhales-0.5.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pywhales 0.5.0
File Interpreter ABI Platform Actions
pywhales-0.5.0-py3-none-any.whl Python 3 none any Details for pywhales-0.5.0-py3-none-any.whl

Total release size: 496.6 kB

Release files / pywhales-0.5.0.tar.gz

Download URL pywhales-0.5.0.tar.gz
Size 257.7 kB
Tags Source
SHA-256 checksum
How to use checksums
628df6219b64f777e6c7de61253df73dc918b43883aed2fb1878dfa9812383e0
BLAKE2b-256 checksum
How to use checksums
4a2408a506985d2c1ac048392fef1d49be16ac5b58e9f26b89c0c6746ba81440
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / pywhales-0.5.0-py3-none-any.whl

Download URL pywhales-0.5.0-py3-none-any.whl
Size 238.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bb03c932eaf585d0755ceacfc5b9fa0d95fb49bfe211b7bdabd87d23d90d4dfa
BLAKE2b-256 checksum
How to use checksums
6783631ed4372f6ae2190ecc90452d44c124582a6eb3281002a36f84527e87a5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for pywhales 0.5.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page