Skip to content

Commit b0fe38f

Browse files
authored
operations.docker: add extra parameters (#1593)
1 parent 2f8df17 commit b0fe38f

10 files changed

Lines changed: 219 additions & 0 deletions

src/pyinfra/operations/docker.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ def container(
3535
start: bool = True,
3636
restart_policy: str | None = None,
3737
auto_remove: bool = False,
38+
mounts: list[str] | None = None,
39+
privileged: bool = False,
40+
hostname: str | None = None,
41+
entrypoint: str | None = None,
42+
user: str | None = None,
43+
cpus: float | None = None,
44+
memory: str | None = None,
45+
extra_args: list[str] | None = None,
3846
dns: list[str] | None = None,
3947
):
4048
"""
@@ -54,6 +62,14 @@ def container(
5462
+ start: start or stop the container
5563
+ restart_policy: restart policy to apply when a container exits
5664
+ auto_remove: automatically remove the container and its associated anonymous volumes when it exits
65+
+ mounts: list of ``--mount`` specifications (e.g. ``type=bind,source=/src,target=/app``)
66+
+ privileged: give extended privileges to the container
67+
+ hostname: container hostname
68+
+ entrypoint: override the default entrypoint
69+
+ user: username or UID to run as
70+
+ cpus: number of CPUs (e.g. ``1.5``)
71+
+ memory: memory limit (e.g. ``512m``, ``1g``)
72+
+ extra_args: list of additional raw arguments passed to ``docker container create``
5773
+ dns: list of dns servers to be used by the container
5874
5975
**Examples:**
@@ -76,6 +92,21 @@ def container(
7692
auto_remove=True,
7793
)
7894
95+
# Run a container with mounts and resource limits
96+
docker.container(
97+
name="Deploy app container",
98+
container="myapp",
99+
image="myapp:latest",
100+
mounts=["type=bind,source=/host/data,target=/app/data"],
101+
privileged=True,
102+
hostname="myapp-host",
103+
entrypoint="/bin/sh",
104+
user="1000:1000",
105+
cpus=2.0,
106+
memory="512m",
107+
extra_args=["--cap-add", "NET_ADMIN"],
108+
)
109+
79110
# Stop a container
80111
docker.container(
81112
name="Stop Nginx container",
@@ -102,6 +133,14 @@ def container(
102133
pull_always,
103134
restart_policy,
104135
auto_remove,
136+
mounts or list(),
137+
privileged,
138+
hostname,
139+
entrypoint,
140+
user,
141+
cpus,
142+
memory,
143+
extra_args or list(),
105144
dns or list(),
106145
)
107146
existent_container = host.get_fact(DockerContainer, object_id=container)

src/pyinfra/operations/util/docker.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,14 @@ class ContainerSpec:
168168
pull_always: bool = False
169169
restart_policy: str | None = None
170170
auto_remove: bool = False
171+
mounts: list[str] = field(default_factory=list)
172+
privileged: bool = False
173+
hostname: str | None = None
174+
entrypoint: str | None = None
175+
user: str | None = None
176+
cpus: float | None = None
177+
memory: str | None = None
178+
extra_args: list[str] = field(default_factory=list)
171179
dns: list[str] = field(default_factory=list)
172180

173181
def container_create_args(self):
@@ -181,6 +189,9 @@ def container_create_args(self):
181189
for volume in self.volumes:
182190
args.append("-v {0}".format(volume))
183191

192+
for mount in self.mounts:
193+
args.append("--mount {0}".format(mount))
194+
184195
for env_var in self.env_vars:
185196
args.append("-e {0}".format(env_var))
186197

@@ -199,6 +210,27 @@ def container_create_args(self):
199210
if self.auto_remove:
200211
args.append("--rm")
201212

213+
if self.privileged:
214+
args.append("--privileged")
215+
216+
if self.hostname is not None:
217+
args.append("--hostname {0}".format(self.hostname))
218+
219+
if self.entrypoint is not None:
220+
args.append("--entrypoint {0}".format(self.entrypoint))
221+
222+
if self.user is not None:
223+
args.append("--user {0}".format(self.user))
224+
225+
if self.cpus is not None:
226+
args.append("--cpus {0}".format(self.cpus))
227+
228+
if self.memory is not None:
229+
args.append("--memory {0}".format(self.memory))
230+
231+
for extra_arg in self.extra_args:
232+
args.append(extra_arg)
233+
202234
for dns in self.dns:
203235
args.append("--dns {0}".format(dns))
204236

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"kwargs": {
3+
"container": "myapp",
4+
"image": "myapp:latest",
5+
"ports": ["8080:80"],
6+
"mounts": ["type=bind,source=/host/data,target=/app/data"],
7+
"privileged": true,
8+
"hostname": "myapp-host",
9+
"entrypoint": "/bin/sh",
10+
"user": "1000:1000",
11+
"cpus": 1.5,
12+
"memory": "1g",
13+
"extra_args": ["--cap-add", "SYS_PTRACE"],
14+
"present": "true",
15+
"start": false
16+
},
17+
"facts": {
18+
"docker.DockerContainer": {
19+
"object_id=myapp": []
20+
}
21+
},
22+
"commands": [
23+
"docker container create --name myapp -p 8080:80 --mount type=bind,source=/host/data,target=/app/data --privileged --hostname myapp-host --entrypoint /bin/sh --user 1000:1000 --cpus 1.5 --memory 1g --cap-add SYS_PTRACE myapp:latest"
24+
]
25+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"kwargs": {
3+
"container": "myapp",
4+
"image": "myapp:latest",
5+
"cpus": 2.0,
6+
"memory": "512m",
7+
"present": "true",
8+
"start": false
9+
},
10+
"facts": {
11+
"docker.DockerContainer": {
12+
"object_id=myapp": []
13+
}
14+
},
15+
"commands": [
16+
"docker container create --name myapp --cpus 2.0 --memory 512m myapp:latest"
17+
]
18+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"kwargs": {
3+
"container": "myapp",
4+
"image": "myapp:latest",
5+
"entrypoint": "/bin/sh",
6+
"present": "true",
7+
"start": false
8+
},
9+
"facts": {
10+
"docker.DockerContainer": {
11+
"object_id=myapp": []
12+
}
13+
},
14+
"commands": [
15+
"docker container create --name myapp --entrypoint /bin/sh myapp:latest"
16+
]
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"kwargs": {
3+
"container": "myapp",
4+
"image": "myapp:latest",
5+
"extra_args": ["--cap-add", "NET_ADMIN", "--device", "/dev/net/tun"],
6+
"present": "true",
7+
"start": false
8+
},
9+
"facts": {
10+
"docker.DockerContainer": {
11+
"object_id=myapp": []
12+
}
13+
},
14+
"commands": [
15+
"docker container create --name myapp --cap-add NET_ADMIN --device /dev/net/tun myapp:latest"
16+
]
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"kwargs": {
3+
"container": "myapp",
4+
"image": "myapp:latest",
5+
"hostname": "myapp-host",
6+
"present": "true",
7+
"start": false
8+
},
9+
"facts": {
10+
"docker.DockerContainer": {
11+
"object_id=myapp": []
12+
}
13+
},
14+
"commands": [
15+
"docker container create --name myapp --hostname myapp-host myapp:latest"
16+
]
17+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"kwargs": {
3+
"container": "myapp",
4+
"image": "myapp:latest",
5+
"mounts": [
6+
"type=bind,source=/host/data,target=/app/data",
7+
"type=tmpfs,target=/tmp"
8+
],
9+
"present": "true",
10+
"start": false
11+
},
12+
"facts": {
13+
"docker.DockerContainer": {
14+
"object_id=myapp": []
15+
}
16+
},
17+
"commands": [
18+
"docker container create --name myapp --mount type=bind,source=/host/data,target=/app/data --mount type=tmpfs,target=/tmp myapp:latest"
19+
]
20+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"kwargs": {
3+
"container": "myapp",
4+
"image": "myapp:latest",
5+
"privileged": true,
6+
"present": "true",
7+
"start": false
8+
},
9+
"facts": {
10+
"docker.DockerContainer": {
11+
"object_id=myapp": []
12+
}
13+
},
14+
"commands": [
15+
"docker container create --name myapp --privileged myapp:latest"
16+
]
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"kwargs": {
3+
"container": "myapp",
4+
"image": "myapp:latest",
5+
"user": "1000:1000",
6+
"present": "true",
7+
"start": false
8+
},
9+
"facts": {
10+
"docker.DockerContainer": {
11+
"object_id=myapp": []
12+
}
13+
},
14+
"commands": [
15+
"docker container create --name myapp --user 1000:1000 myapp:latest"
16+
]
17+
}

0 commit comments

Comments
 (0)