Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make images.build async #3158

Open
agowa opened this issue Jul 12, 2023 · 1 comment
Open

Make images.build async #3158

agowa opened this issue Jul 12, 2023 · 1 comment

Comments

@agowa
Copy link

agowa commented Jul 12, 2023

Builds take a long time and currently the Image.build function does block until the build is complete and worse, as it returns a trouble with the generator as the 2nd argument it'll also not allow to output the build log in real-time to the terminal, but only once it returned.

Improvement suggestion, add an async version of images.build

Example usage:

import docker
_client = docker.from_env()
with _client.images.build_async(path="/tmp/foo", pull=True, rm=True, quiet=False) as _builder:
  _builder.start()
  for _line in _builder.logs:
    print(_line)
  if _builder.status == "failed":
    do_something()

Functions and properties to control when the builder job is actually executed: start/stop/pause/resume/status and ".logs" as a generator for the build log with real-time updates.

@naufalafif
Copy link

you might want to switch to aiodocker (async docker) https://aiodocker.readthedocs.io/en/latest/images.html

or use low-level-api version with thread instead.

import docker
import threading

def build_image(client, path, tag):
    print(f"Building image {tag}...")
    logs = client.api.build(path=path, tag=tag, rm=True)
    for line in logs:
        print(line.decode().strip())

def main():
    # Create Docker client
    client = docker.from_env()

    # Define image paths and tags
    images = [
        {"path": "/path/to/image1", "tag": "image1:latest"},
        {"path": "/path/to/image2", "tag": "image2:latest"},
        {"path": "/path/to/image3", "tag": "image3:latest"},
    ]

    # Create threads for each image build
    threads = []
    for image in images:
        thread = threading.Thread(target=build_image, args=(client, image["path"], image["tag"]))
        threads.append(thread)
        thread.start()

    # Wait for all threads to complete
    for thread in threads:
        thread.join()

    print("All image builds completed.")

if __name__ == "__main__":
    main()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants