Aiodocker
Aiodocker is a Python library providing an asynchronous HTTP API wrapper for Docker, built upon `asyncio` and `aiohttp`. It allows programmatic interaction with Docker daemons from Python applications. Currently at version 0.26.0, the library is under active development with regular releases, ensuring compatibility with recent Docker API versions and Python features.
Warnings
- breaking Version 0.25.0 dropped support for Python 3.9. It now requires Python >= 3.10. Additionally, it updated minimum dependencies for `aiohttp` to 3.10 and `async-timeout` to 5.0.
- breaking In version 0.25.0, `DockerContainer.{stop, restart, kill, delete}()` methods replaced `**kwargs` with explicit parameters. Specifically, the server-side stop timeout is now `t` and the client-side request timeout is `timeout`. If you were passing `timeout` for server-side behavior, it must now be `t`.
- breaking The method `docker.images.get` was renamed to `docker.images.inspect` and support for Docker API version 17.06 was removed in older versions.
- gotcha Unlike `docker-py`, `aiodocker` raises a generic `aiodocker.exceptions.DockerError` for 'Not Found' scenarios (e.g., image or container not existing). To check for a 'Not Found' error, you must inspect the `status` attribute of the exception.
- gotcha Setting individual float timeouts per-API call is highly discouraged starting from version 0.25.0. The recommended approach for managing timeouts is via Python's standard library `asyncio.timeout()` async context manager, for better composability and consistency.
Install
-
pip install aiodocker
Imports
- Docker
from aiodocker import Docker
- DockerError
from aiodocker.exceptions import DockerError
Quickstart
import asyncio
import aiodocker
async def list_and_run_containers():
docker = aiodocker.Docker()
try:
print('== Images ==')
for image in (await docker.images.list()):
tags = image['RepoTags'][0] if image['RepoTags'] else ''
print(image['Id'], tags)
print('== Containers ==')
for container in (await docker.containers.list()):
print(f" {container._id}")
print('== Running a hello-world container ==')
container = await docker.containers.create_or_replace(
config={
'Cmd': ['/bin/ash', '-c', 'echo "hello world"'],
'Image': 'alpine:latest',
},
name='testing',
)
await container.start()
logs = await container.log(stdout=True)
print(''.join(logs))
await container.delete(force=True)
finally:
await docker.close()
if __name__ == "__main__":
asyncio.run(list_and_run_containers())