Skip to content

Commit 3988181

Browse files
committed
docker.container: Support container args
This change adds an `args` parameter to the `docker.container` operation that passes said supplied args to the container at creation time. This allows the operation to support container images that have an entrypoint expecting to receive additional arguments, without needing to build+push a custom image that embeds said arguments.
1 parent e360626 commit 3988181

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

pyinfra/operations/docker.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
def container(
1616
container,
1717
image="",
18+
args=None,
1819
ports=None,
1920
networks=None,
2021
volumes=None,
@@ -28,6 +29,7 @@ def container(
2829
Manage Docker containers
2930
3031
+ container: name to identify the container
32+
+ args: list of command-line args to supply to the image
3133
+ image: container image and tag ex: nginx:alpine
3234
+ networks: network list to attach on container
3335
+ ports: port list to expose
@@ -72,6 +74,7 @@ def container(
7274

7375
want_spec = ContainerSpec(
7476
image,
77+
args or list(),
7578
ports or list(),
7679
networks or list(),
7780
volumes or list(),

pyinfra/operations/util/docker.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
@dataclasses.dataclass
88
class ContainerSpec:
99
image: str = ""
10+
args: List[str] = dataclasses.field(default_factory=list)
1011
ports: List[str] = dataclasses.field(default_factory=list)
1112
networks: List[str] = dataclasses.field(default_factory=list)
1213
volumes: List[str] = dataclasses.field(default_factory=list)
@@ -31,6 +32,7 @@ def container_create_args(self):
3132
args.append("--pull always")
3233

3334
args.append(self.image)
35+
args.extend(self.args)
3436

3537
return args
3638

tests/operations/docker.container/add_and_start_no_existent_container.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
"kwargs": {
33
"container": "nginx",
44
"image": "nginx:alpine",
5+
"args": [
6+
"nginx-debug",
7+
"-g",
8+
"'daemon off;'"
9+
],
510
"ports": [
611
"80:80"
712
],
@@ -14,7 +19,7 @@
1419
}
1520
},
1621
"commands": [
17-
"docker container create --name nginx -p 80:80 nginx:alpine",
22+
"docker container create --name nginx -p 80:80 nginx:alpine nginx-debug -g 'daemon off;'",
1823
"docker container start nginx"
1924
]
2025
}

0 commit comments

Comments
 (0)