From a109ab1890430f579db30b54163fb13bcd6664ff Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Mon, 26 Jun 2023 20:48:55 -0700 Subject: [PATCH] aider: Added check for existing Docker image before building. # Context: USER: in `build_docker` check if the image is already built and return ASSISTANT: To check if the Docker image is already built and return if it exists, we can use the `docker images` command and parse its output. Here's how to modify the `build_docker` function: ```python benchmark/benchmark.py <<<<<<< ORIGINAL def build_docker(): command = [ "docker", "build", "--quiet", "-t", "benchmark", "-f", "benchmark/Dockerfile", "/dev/null", ] print(" ".join(command)) try: result = subprocess.run( command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, ) res = result.stdout print(res) except subprocess.CalledProcessError as e: res = f"Failed to build Docker image: {e.output}" return res ======= def build_docker(): image_name = "benchmark" check_command = ["docker", "images", "-q", image_name] check_result = subprocess.run(check_command, stdout=subprocess.PIPE, text=True) if check_result.stdout.strip(): print(f"Docker image '{image_name}' already exists, skipping build.") return command = [ "docker", "build", "--quiet", "-t", image_name, "-f", "benchmark/Dockerfile", "/dev/null", ] print(" ".join(command)) try: result = subprocess.run( command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, ) res = result.stdout print(res) except subprocess.CalledProcessError as e: res = f"Failed to build Docker image: {e.output}" return res >>>>>>> UPDATED ``` This modification adds a check for the existence of the Docker image before attempting to build it. If the image already exists, the function prints a message and returns early. --- benchmark/benchmark.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/benchmark/benchmark.py b/benchmark/benchmark.py index fd87ce803..c8bc653ee 100755 --- a/benchmark/benchmark.py +++ b/benchmark/benchmark.py @@ -386,12 +386,19 @@ def run_unit_tests(testdir, history_fname): def build_docker(): + image_name = "benchmark" + check_command = ["docker", "images", "-q", image_name] + check_result = subprocess.run(check_command, stdout=subprocess.PIPE, text=True) + if check_result.stdout.strip(): + print(f"Docker image '{image_name}' already exists, skipping build.") + return + command = [ "docker", "build", "--quiet", "-t", - "benchmark", + image_name, "-f", "benchmark/Dockerfile", "/dev/null",