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

Expose dockerfile and args arguments under build #477

Merged
merged 14 commits into from
Sep 28, 2023
3 changes: 3 additions & 0 deletions python_on_whales/components/compose/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ class DependencyCondition(BaseModel):

class ComposeServiceBuild(BaseModel):
context: Optional[Path] = None
dockerfile: Optional[Path] = None
args: Optional[Dict[str, Any]] = None
labels: Optional[Dict[str, Any]] = None


class ComposeServicePort(BaseModel):
Expand Down
25 changes: 25 additions & 0 deletions tests/python_on_whales/components/test-build-args.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: "3.7"

services:
my_service:
build:
context: my_service_build
dockerfile: docker/somefile.dockerfile
args:
python_version: "3.78"
python_version_1: "3.78"
labels:
com.example.description: "Accounting webapp"
com.example.department: "Finance"
image: "some_random_image"
command: ping -c 7 www.google.com
ports:
- "5000:5000"
volumes:
- /tmp:/tmp
- dodo:/dodo
environment:
- DATADOG_HOST=something

volumes:
dodo: {}
41 changes: 41 additions & 0 deletions tests/python_on_whales/components/test_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -888,3 +888,44 @@ def test_docker_compose_run_build():
== docker.image.list("some_random_image")[0].repo_tags[0].split(":latest")[0]
)
docker.image.remove("some_random_image", force=True)


def test_build_args():
compose_file = (
PROJECT_ROOT / "tests/python_on_whales/components/test-build-args.yml"
)
docker = DockerClient(compose_files=[compose_file])
config = docker.compose.config()

assert (
config.services["my_service"].build.context
== (compose_file.parent / "my_service_build").absolute()
)
assert config.services["my_service"].build.dockerfile == Path(
"docker/somefile.dockerfile"
)
assert config.services["my_service"].build.args == {
"python_version": "3.78",
"python_version_1": "3.78",
}
assert config.services["my_service"].build.labels == {
"com.example.description": "Accounting webapp",
"com.example.department": "Finance",
}
assert config.services["my_service"].image == "some_random_image"
assert config.services["my_service"].command == [
"ping",
"-c",
"7",
"www.google.com",
]

assert config.services["my_service"].ports[0].published == 5000
assert config.services["my_service"].ports[0].target == 5000

assert config.services["my_service"].volumes[0].source == "/tmp"
assert config.services["my_service"].volumes[0].target == "/tmp"
assert config.services["my_service"].volumes[1].source == "dodo"
assert config.services["my_service"].volumes[1].target == "/dodo"

assert config.services["my_service"].environment == {"DATADOG_HOST": "something"}
Loading