Skip to content

patch dockerFile build #1979

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions cwltool/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
58 changes: 58 additions & 0 deletions tests/test_tmpdir.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
Loading