Skip to content
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
11 changes: 11 additions & 0 deletions src/pyinfra/operations/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def container(
restart_policy: str | None = None,
auto_remove: bool = False,
dns: list[str] | None = None,
command: str | None = None,
):
"""
Manage Docker containers
Expand All @@ -55,6 +56,7 @@ def container(
+ restart_policy: restart policy to apply when a container exits
+ auto_remove: automatically remove the container and its associated anonymous volumes when it exits
+ dns: list of dns servers to be used by the container
+ command: custom command to run on container start

**Examples:**

Expand Down Expand Up @@ -89,6 +91,14 @@ def container(
container="nginx",
start=True,
)

# Run a custom command on container start
# Note: you can omit the shell (sh -c) to use the default shell of the container
docker.container(
name="Run a custom command",
container="alpine",
command="sh -c 'echo Whatever you want'",
)
"""

want_spec = ContainerSpec(
Expand All @@ -103,6 +113,7 @@ def container(
restart_policy,
auto_remove,
dns or list(),
command,
)
existent_container = host.get_fact(DockerContainer, object_id=container)

Expand Down
3 changes: 3 additions & 0 deletions src/pyinfra/operations/util/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ class ContainerSpec:
restart_policy: str | None = None
auto_remove: bool = False
dns: list[str] = field(default_factory=list)
command: str | None = None

def container_create_args(self):
args = []
Expand Down Expand Up @@ -203,6 +204,8 @@ def container_create_args(self):
args.append("--dns {0}".format(dns))

args.append(self.image)
if self.command:
args.append(self.command)

return args

Expand Down
17 changes: 17 additions & 0 deletions tests/operations/docker.container/add_container_with_command.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"kwargs": {
"container": "nginx",
"image": "nginx:alpine",
"present": "true",
"start": false,
"command": "sh -c 'echo leroy jenkins'"
},
"facts": {
"docker.DockerContainer": {
"object_id=nginx": []
}
},
"commands": [
"docker container create --name nginx nginx:alpine sh -c 'echo leroy jenkins'"
]
}
Loading