From a4d219b1feb0ce860e315237b60c133f4a8c586c Mon Sep 17 00:00:00 2001 From: Heiru Wu Date: Fri, 26 Apr 2024 05:05:48 +0800 Subject: [PATCH 1/2] feat(ray): adapt to native docker client instead of docker sdk --- instill/helpers/.dockerignore | 17 +++++++++++ instill/helpers/Dockerfile | 3 +- instill/helpers/build.py | 54 ++++++++++++++++++++++------------- instill/helpers/push.py | 28 ++++++------------ 4 files changed, 60 insertions(+), 42 deletions(-) create mode 100644 instill/helpers/.dockerignore diff --git a/instill/helpers/.dockerignore b/instill/helpers/.dockerignore new file mode 100644 index 0000000..4522d57 --- /dev/null +++ b/instill/helpers/.dockerignore @@ -0,0 +1,17 @@ +# The .dockerignore file excludes files from the container build process. +# +# https://docs.docker.com/engine/reference/builder/#dockerignore-file + +# Exclude Git files +.git +.github +.gitignore + +# Exclude Python cache files +__pycache__ +.mypy_cache +.pytest_cache +.ruff_cache + +# Exclude Python virtual environment +/venv diff --git a/instill/helpers/Dockerfile b/instill/helpers/Dockerfile index 65762c3..75bb82a 100644 --- a/instill/helpers/Dockerfile +++ b/instill/helpers/Dockerfile @@ -5,10 +5,9 @@ ARG TARGET_ARCH_SUFFIX FROM rayproject/ray:${RAY_VERSION}-py${PYTHON_VERSION}${CUDA_SUFFIX}${TARGET_ARCH_SUFFIX} -ARG PACKAGES - RUN sudo apt-get update && sudo apt-get install curl -y +ARG PACKAGES RUN for package in ${PACKAGES}; do \ pip install --default-timeout=1000 --no-cache-dir $package; \ done; diff --git a/instill/helpers/build.py b/instill/helpers/build.py index 0d0783e..c1d0476 100644 --- a/instill/helpers/build.py +++ b/instill/helpers/build.py @@ -4,8 +4,8 @@ import platform import shutil import tempfile +import subprocess -import docker import ray import yaml @@ -34,7 +34,6 @@ required=False, ) - client = docker.from_env() try: args = parser.parse_args() @@ -69,31 +68,46 @@ shutil.copyfile( __file__.replace("build.py", "Dockerfile"), f"{tmpdir}/Dockerfile" ) + shutil.copyfile( + __file__.replace("build.py", ".dockerignore"), f"{tmpdir}/.dockerignore" + ) shutil.copytree(os.getcwd(), tmpdir, dirs_exist_ok=True) target_arch_suffix = "-aarch64" if args.target_arch == "arm64" else "" + Logger.i("[Instill Builder] Building model image...") - img, logs = client.images.build( - path=tmpdir, - rm=True, - pull=True, - nocache=args.no_cache, - platform=f"linux/{args.target_arch}", - tag=f"{repo}:{tag}", - buildargs={ - "TARGET_ARCH_SUFFIX": target_arch_suffix, - "RAY_VERSION": ray_version, - "PYTHON_VERSION": python_version, - "CUDA_SUFFIX": cuda_suffix, - "PACKAGES": packages_str, - }, - quiet=False, + command = [ + "docker", + "buildx", + "build", + "--build-arg", + f"TARGET_ARCH_SUFFIX={target_arch_suffix}", + "--build-arg", + f"RAY_VERSION={ray_version}", + "--build-arg", + f"PYTHON_VERSION={python_version}", + "--build-arg", + f"CUDA_SUFFIX={cuda_suffix}", + "--build-arg", + f"PACKAGES={packages_str}", + "--platform", + f"linux/{args.target_arch}", + "-t", + f"{repo}:{tag}", + tmpdir, + "--load", + ] + if args.no_cache: + command.append("--no-cache") + subprocess.run( + command, + check=True, ) - for line in logs: - print(*line.values()) Logger.i(f"[Instill Builder] {repo}:{tag} built") - except Exception as e: + except subprocess.CalledProcessError as e: Logger.e("[Instill Builder] Build failed") + except Exception as e: + Logger.e("[Instill Builder] Prepare failed") Logger.e(e) finally: Logger.i("[Instill Builder] Done") diff --git a/instill/helpers/push.py b/instill/helpers/push.py index 62cae6d..24c39ea 100644 --- a/instill/helpers/push.py +++ b/instill/helpers/push.py @@ -1,14 +1,12 @@ import argparse import types - -import docker +import subprocess import yaml from instill.utils.logger import Logger if __name__ == "__main__": Logger.i("[Instill Builder] Setup docker...") - client = docker.from_env() parser = argparse.ArgumentParser() parser.add_argument( @@ -31,26 +29,16 @@ repo = config["repo"] tag = config["tag"] - img = client.images.get(name=f"{repo}:{tag}") - img.tag(f"{registry}/{repo}", tag) + subprocess.run( + ["docker", "tag", f"{repo}:{tag}", f"{registry}/{repo}:{tag}"], check=True + ) Logger.i("[Instill Builder] Pushing model image...") - logs = client.images.push(f"{registry}/{repo}", tag=tag) - if isinstance(logs, types.GeneratorType): - for line in logs: - print(*line.values()) - elif isinstance(logs, list): - for line in logs: - if "errorDetail" in line: - raise RuntimeError(line["errorDetail"]["message"]) - print(line) - else: - if "errorDetail" in logs: - err = logs.split('{"errorDetail":{"message":', 1)[1][1:-4] - raise RuntimeError(err) - print(logs) + subprocess.run(["docker", "push", f"{registry}/{repo}:{tag}"], check=True) Logger.i(f"[Instill Builder] {registry}/{repo}:{tag} pushed") - except Exception as e: + except subprocess.CalledProcessError as e: Logger.e("[Instill Builder] Push failed") + except Exception as e: + Logger.e("[Instill Builder] Prepare failed") Logger.e(e) finally: Logger.i("[Instill Builder] Done") From 5bb4f45c3fc9a213fc76596d940952c159dece32 Mon Sep 17 00:00:00 2001 From: Heiru Wu Date: Fri, 26 Apr 2024 05:06:50 +0800 Subject: [PATCH 2/2] chore: remove docker and fix imports --- instill/helpers/build.py | 2 +- instill/helpers/push.py | 2 +- poetry.lock | 36 +++++++----------------------------- pyproject.toml | 1 - 4 files changed, 9 insertions(+), 32 deletions(-) diff --git a/instill/helpers/build.py b/instill/helpers/build.py index c1d0476..1b2a618 100644 --- a/instill/helpers/build.py +++ b/instill/helpers/build.py @@ -3,8 +3,8 @@ import os import platform import shutil -import tempfile import subprocess +import tempfile import ray import yaml diff --git a/instill/helpers/push.py b/instill/helpers/push.py index 24c39ea..de49b6e 100644 --- a/instill/helpers/push.py +++ b/instill/helpers/push.py @@ -1,6 +1,6 @@ import argparse -import types import subprocess + import yaml from instill.utils.logger import Logger diff --git a/poetry.lock b/poetry.lock index 6ab148d..3b27cad 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,4 +1,4 @@ -# This file is automatically @generated by Poetry 1.8.2 and should not be changed by hand. +# This file is automatically @generated by Poetry 1.7.1 and should not be changed by hand. [[package]] name = "aiohttp" @@ -233,8 +233,8 @@ files = [ lazy-object-proxy = ">=1.4.0" typing-extensions = {version = ">=4.0.0", markers = "python_version < \"3.11\""} wrapt = [ - {version = ">=1.11,<2", markers = "python_version < \"3.11\""}, {version = ">=1.14,<2", markers = "python_version >= \"3.11\""}, + {version = ">=1.11,<2", markers = "python_version < \"3.11\""}, ] [[package]] @@ -758,10 +758,10 @@ isort = ">=4.3.21,<6.0" jinja2 = ">=2.10.1,<4.0" packaging = "*" pydantic = [ - {version = ">=1.5.1,<2.4.0 || >2.4.0,<3.0", extras = ["email"], markers = "python_version < \"3.10\""}, - {version = ">=1.9.0,<2.4.0 || >2.4.0,<3.0", extras = ["email"], markers = "python_version >= \"3.10\" and python_version < \"3.11\""}, {version = ">=1.10.0,<2.0.0 || >2.0.0,<2.0.1 || >2.0.1,<2.4.0 || >2.4.0,<3.0", extras = ["email"], markers = "python_version >= \"3.12\" and python_version < \"4.0\""}, {version = ">=1.10.0,<2.4.0 || >2.4.0,<3.0", extras = ["email"], markers = "python_version >= \"3.11\" and python_version < \"3.12\""}, + {version = ">=1.5.1,<2.4.0 || >2.4.0,<3.0", extras = ["email"], markers = "python_version < \"3.10\""}, + {version = ">=1.9.0,<2.4.0 || >2.4.0,<3.0", extras = ["email"], markers = "python_version >= \"3.10\" and python_version < \"3.11\""}, ] pyyaml = ">=6.0.1" toml = {version = ">=0.10.0,<1.0.0", markers = "python_version < \"3.11\""} @@ -860,27 +860,6 @@ idna = ["idna (>=3.6)"] trio = ["trio (>=0.23)"] wmi = ["wmi (>=1.5.1)"] -[[package]] -name = "docker" -version = "7.0.0" -description = "A Python library for the Docker Engine API." -optional = false -python-versions = ">=3.8" -files = [ - {file = "docker-7.0.0-py3-none-any.whl", hash = "sha256:12ba681f2777a0ad28ffbcc846a69c31b4dfd9752b47eb425a274ee269c5e14b"}, - {file = "docker-7.0.0.tar.gz", hash = "sha256:323736fb92cd9418fc5e7133bc953e11a9da04f4483f828b527db553f1e7e5a3"}, -] - -[package.dependencies] -packaging = ">=14.0" -pywin32 = {version = ">=304", markers = "sys_platform == \"win32\""} -requests = ">=2.26.0" -urllib3 = ">=1.26.0" - -[package.extras] -ssh = ["paramiko (>=2.4.3)"] -websockets = ["websocket-client (>=1.3.0)"] - [[package]] name = "docopt" version = "0.6.2" @@ -2966,8 +2945,8 @@ files = [ astroid = ">=2.12.13,<=2.14.0-dev0" colorama = {version = ">=0.4.5", markers = "sys_platform == \"win32\""} dill = [ - {version = ">=0.2", markers = "python_version < \"3.11\""}, {version = ">=0.3.6", markers = "python_version >= \"3.11\""}, + {version = ">=0.2", markers = "python_version < \"3.11\""}, ] isort = ">=4.2.5,<6" mccabe = ">=0.6,<0.8" @@ -3172,7 +3151,6 @@ files = [ {file = "PyYAML-6.0.1-cp311-cp311-win_amd64.whl", hash = "sha256:bf07ee2fef7014951eeb99f56f39c9bb4af143d8aa3c21b1677805985307da34"}, {file = "PyYAML-6.0.1-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:855fb52b0dc35af121542a76b9a84f8d1cd886ea97c84703eaa6d88e37a2ad28"}, {file = "PyYAML-6.0.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:40df9b996c2b73138957fe23a16a4f0ba614f4c0efce1e9406a184b6d07fa3a9"}, - {file = "PyYAML-6.0.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a08c6f0fe150303c1c6b71ebcd7213c2858041a7e01975da3a99aed1e7a378ef"}, {file = "PyYAML-6.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6c22bec3fbe2524cde73d7ada88f6566758a8f7227bfbf93a408a9d86bcc12a0"}, {file = "PyYAML-6.0.1-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:8d4e9c88387b0f5c7d5f281e55304de64cf7f9c0021a3525bd3b1c542da3b0e4"}, {file = "PyYAML-6.0.1-cp312-cp312-win32.whl", hash = "sha256:d483d2cdf104e7c9fa60c544d92981f12ad66a457afae824d146093b8c294c54"}, @@ -3367,8 +3345,8 @@ filelock = "*" frozenlist = "*" gpustat = {version = ">=1.0.0", optional = true, markers = "extra == \"serve\""} grpcio = [ - {version = ">=1.32.0", optional = true, markers = "python_version < \"3.10\" and extra == \"serve\""}, {version = ">=1.42.0", optional = true, markers = "python_version >= \"3.10\" and extra == \"serve\""}, + {version = ">=1.32.0", optional = true, markers = "python_version < \"3.10\" and extra == \"serve\""}, ] jsonschema = "*" msgpack = ">=1.0.0,<2.0.0" @@ -4422,4 +4400,4 @@ testing = ["big-O", "jaraco.functools", "jaraco.itertools", "more-itertools", "p [metadata] lock-version = "2.0" python-versions = ">=3.8,<3.13" -content-hash = "3c012e0c75fe57fe6ccb5f334e203513a3683dee97c91d956f3add972635bc2e" +content-hash = "44217531e536ec7373d2bc3e5504845359e199ec3f6d4c63555becefd10425f8" diff --git a/pyproject.toml b/pyproject.toml index 04b1a85..eedcc4e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -39,7 +39,6 @@ pydantic = ">=1.10.13" pillow = "^10.1.0" ray = {version = "2.9.3", extras = ["serve"]} jsonschema = "^4.20.0" -docker = "^7.0.0" [tool.poetry.dev-dependencies]