diff --git a/aclimatise/execution/docker.py b/aclimatise/execution/docker.py index 51ddc40..45d2752 100644 --- a/aclimatise/execution/docker.py +++ b/aclimatise/execution/docker.py @@ -8,6 +8,7 @@ from docker.utils.socket import consume_socket_output, demux_adaptor, frames_iter from aclimatise.execution.help import CliHelpExecutor +from aclimatise.model import Command def read_socket(sock, timeout: int = None) -> Tuple[bytes, bytes]: @@ -40,9 +41,27 @@ class DockerExecutor(CliHelpExecutor): An executor that runs the commands on an already-running docker Container (not an Image!) """ - def __init__(self, container: "docker.models.containers.Container", **kwargs): + def __init__( + self, container: "docker.models.containers.Container", save_image=True, **kwargs + ): + """ + :param container: The object from the Docker API that represents the running container to run inside + :param save_image: If true (default), save the image name on the command, meaning that the resulting tool + definitions also use this Docker image + """ super().__init__(**kwargs) self.container = container + self.save_image = save_image + + def convert( + self, + cmd: List[str], + ) -> Command: + # Use the existing function, but patch in the docker image + cmd = super().convert(cmd) + if self.save_image: + cmd.docker_image = self.container.image.tags[0] + return cmd def execute(self, command: List[str]) -> str: _, sock = self.container.exec_run( diff --git a/docs/changes.rst b/docs/changes.rst index 0e76718..ea429b4 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -14,6 +14,7 @@ Breaking Changes New Features ************ * Add ``docker_image`` field to ``Command`` +* The ``DockerExecutor`` has been updated to automatically set this field based on the Docker image being used by it * Add `Janis `_ converter (`#73 `_) Bug Fixes diff --git a/test/executors/test_docker.py b/test/executors/test_docker.py index 5de2c66..3926e0c 100644 --- a/test/executors/test_docker.py +++ b/test/executors/test_docker.py @@ -4,6 +4,20 @@ from aclimatise.execution.docker import DockerExecutor +@pytest.mark.timeout(360) +def test_docker_image_saved(bwamem_help): + client = docker.from_env() + container = client.containers.run( + "biocontainers/bwa:v0.7.17_cv1", + entrypoint=["sleep", "999999999"], + detach=True, + ) + + exec = DockerExecutor(container) + cmd = exec.convert(["bwa", "mem"]) + assert cmd.docker_image == "biocontainers/bwa:v0.7.17_cv1" + + def test_docker(bwamem_help): client = docker.from_env() container = client.containers.run(