From a0f982fd7ea500ba25868a949babb5f2c272a2cb Mon Sep 17 00:00:00 2001 From: Brandon Duane Walker Date: Mon, 26 Feb 2024 16:00:30 -0500 Subject: [PATCH 1/2] patch dockerFile build --- cwltool/docker.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/cwltool/docker.py b/cwltool/docker.py index 4114173b2..d0f628b15 100644 --- a/cwltool/docker.py +++ b/cwltool/docker.py @@ -127,26 +127,27 @@ def get_image( except (OSError, subprocess.CalledProcessError, UnicodeError): pass + cmd: List[str] = [] + if "dockerFile" in docker_requirement: + dockerfile_dir = create_tmp_dir(tmp_outdir_prefix) + with open(os.path.join(dockerfile_dir, "Dockerfile"), "w") as dfile: + dfile.write(docker_requirement["dockerFile"]) + cmd = [ + self.docker_exec, + "build", + "--tag=%s" % str(docker_requirement["dockerImageId"]), + dockerfile_dir, + ] + _logger.info(str(cmd)) + subprocess.check_call(cmd, stdout=sys.stderr) # nosec + found = True + if (force_pull or not found) and pull_image: - cmd: List[str] = [] if "dockerPull" in docker_requirement: cmd = [self.docker_exec, "pull", str(docker_requirement["dockerPull"])] _logger.info(str(cmd)) subprocess.check_call(cmd, stdout=sys.stderr) # nosec found = True - elif "dockerFile" in docker_requirement: - dockerfile_dir = create_tmp_dir(tmp_outdir_prefix) - with open(os.path.join(dockerfile_dir, "Dockerfile"), "w") as dfile: - dfile.write(docker_requirement["dockerFile"]) - cmd = [ - self.docker_exec, - "build", - "--tag=%s" % str(docker_requirement["dockerImageId"]), - dockerfile_dir, - ] - _logger.info(str(cmd)) - subprocess.check_call(cmd, stdout=sys.stderr) # nosec - found = True elif "dockerLoad" in docker_requirement: cmd = [self.docker_exec, "load"] _logger.info(str(cmd)) From f43df0f7c5cd68b85f7cd94efa58a6087cf29a11 Mon Sep 17 00:00:00 2001 From: Brandon Duane Walker Date: Tue, 27 Feb 2024 11:39:19 -0500 Subject: [PATCH 2/2] add test for building Dockerfile --- tests/test_tmpdir.py | 58 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/tests/test_tmpdir.py b/tests/test_tmpdir.py index af830834b..2470a2515 100644 --- a/tests/test_tmpdir.py +++ b/tests/test_tmpdir.py @@ -168,6 +168,64 @@ def test_dockerfile_tmpdir_prefix(tmp_path: Path, monkeypatch: pytest.MonkeyPatc assert (subdir / "Dockerfile").exists() +@needs_docker +def test_dockerfile_build(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None: + """Test that DockerCommandLineJob.get_image builds a Dockerfile.""" + (tmp_path / "out").mkdir() + tmp_outdir_prefix = tmp_path / "out" / "1" + (tmp_path / "3").mkdir() + tmpdir_prefix = str(tmp_path / "3" / "ttmp") + runtime_context = RuntimeContext( + {"tmpdir_prefix": tmpdir_prefix, "user_space_docker_cmd": None} + ) + builder = Builder( + {}, + [], + [], + {}, + schema.Names(), + [], + [], + {}, + None, + None, + StdFsAccess, + StdFsAccess(""), + None, + 0.1, + False, + False, + False, + "no_listing", + runtime_context.get_outdir(), + runtime_context.get_tmpdir(), + runtime_context.get_stagedir(), + INTERNAL_VERSION, + "docker", + ) + + docker_image_id = sys._getframe().f_code.co_name + + assert DockerCommandLineJob( + builder, {}, CommandLineTool.make_path_mapper, [], [], "" + ).get_image( + { + "class": "DockerRequirement", + "dockerFile": "FROM debian:stable-slim", + "dockerImageId": docker_image_id, + }, + pull_image=False, + force_pull=False, + tmp_outdir_prefix=str(tmp_outdir_prefix), + ) + output = subprocess.check_output( + ["docker", "images", "--quiet", docker_image_id], stderr=subprocess.STDOUT, text=True + ) + + # If the output is empty, the image doesn't exist + assert output.strip(), f"Docker image {docker_image_id} does not exist" + + @needs_singularity def test_dockerfile_singularity_build(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None: """Test that SingularityCommandLineJob.get_image builds a Dockerfile with Singularity."""