Aiodocker

0.26.0 · active · verified Fri Apr 10

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

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Docker client, list existing images and containers, run a 'hello-world' container, capture its logs, and then clean it up. It showcases basic asynchronous operations with the Docker API.

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())

view raw JSON →