Skip to content
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

Add volumes option to docker.compose.run() (#662) #663

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
10 changes: 9 additions & 1 deletion python_on_whales/components/compose/cli_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from typing_extensions import Literal

import python_on_whales.components.container.cli_wrapper
import python_on_whales.components.volume.cli_wrapper
from python_on_whales.client_config import DockerCLICaller
from python_on_whales.components.compose.models import ComposeConfig, ComposeProject
from python_on_whales.utils import (
Expand Down Expand Up @@ -677,7 +678,9 @@ def run(
service_ports: bool = False,
use_aliases: bool = False,
user: Optional[str] = None,
# volumes: bool = "todo",
volumes: Iterable[
python_on_whales.components.volume.cli_wrapper.VolumeDefinition
] = (),
workdir: Union[None, str, Path] = None,
) -> Union[
str,
Expand All @@ -703,6 +706,8 @@ def run(
to write on it.
stream: Similar to `docker.run(..., stream=True)`.
user: Username or UID, format: `"<name|uid>[:<group|gid>]"`
volumes: Bind mount a volume. Some examples:
`[("/", "/host"), ("/etc/hosts", "/etc/hosts", "rw")]`.
workdir: Working directory inside the container

Returns:
Expand Down Expand Up @@ -746,6 +751,9 @@ def run(
full_cmd.add_flag("--service-ports", service_ports)
full_cmd.add_flag("--use-aliases", use_aliases)
full_cmd.add_simple_arg("--user", user)
for volume_definition in volumes:
volume_definition = tuple(str(x) for x in volume_definition)
full_cmd += ["--volume", ":".join(volume_definition)]
full_cmd.add_simple_arg("--workdir", workdir)
full_cmd.add_args_iterable_or_single("--label", format_mapping_for_cli(labels))
full_cmd.append(service)
Expand Down
39 changes: 39 additions & 0 deletions tests/python_on_whales/components/test_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,45 @@ def test_compose_run_entrypoint():
assert result == "cmd-is-argument-for-echo"


def test_compose_run_volume():
with tempfile.NamedTemporaryFile() as fst, tempfile.NamedTemporaryFile() as snd:
fst.write(b"Hello ")
snd.write(b" Year")
fst.flush()
snd.flush()

result = docker.compose.run(
"dodo",
[
"-c",
"echo New >> /mounted/fst.txt"
"&& echo $(cat /mounted/fst.txt)$(cat /mounted/snd.txt)",
],
volumes=[
(f"{fst.name}", "/mounted/fst.txt"),
(f"{snd.name}", "/mounted/snd.txt", "ro"),
],
remove=True,
tty=False,
)

assert result == "Hello New Year"


def test_compose_run_volume_with_parameter():
with tempfile.NamedTemporaryFile() as read_only_file, pytest.raises(
DockerException,
match=".*can't create /mounted/read_only_file.txt: Read-only file system.*",
):
docker.compose.run(
"dodo",
["-c", "echo test > /mounted/read_only_file.txt"],
volumes=[(f"{read_only_file.name}", "/mounted/read_only_file.txt", "ro")],
remove=True,
tty=False,
)


def test_compose_version():
assert "Docker Compose version v2" in docker.compose.version()

Expand Down