Skip to content

Commit f13cfa7

Browse files
author
Neile Havens
committed
add support for the --user option to docker run
https://docs.docker.com/engine/reference/run/#user Resolves tox-dev#135
1 parent 7b573c5 commit f13cfa7

File tree

5 files changed

+28
-0
lines changed

5 files changed

+28
-0
lines changed

README.rst

+6
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ The ``[docker:container-name]`` section may contain the following directives:
121121
test run until the container reports healthy, and will fail the test
122122
run if it never does so (within the parameters specified).
123123

124+
``user``
125+
The `user<https://docs.docker.com/engine/reference/run/#user>`__ Username
126+
or UID to run commands as inside the container.
127+
124128
Command-Line Arguments
125129
----------------------
126130

@@ -182,6 +186,8 @@ Example
182186
# testing use cases, as this could persist data between test runs
183187
volumes =
184188
bind:rw:/my/own/datadir:/var/lib/postgresql/data
189+
# Run the container under the specified user
190+
user = 1234
185191
186192
[docker:appserv]
187193
# You can use any value that `docker run` would accept as the image

tox_docker/config.py

+2
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ def __init__(
118118
ports: Optional[Collection[Port]] = None,
119119
links: Optional[Collection[Link]] = None,
120120
volumes: Optional[Collection[Volume]] = None,
121+
user: Optional[str] = None,
121122
) -> None:
122123
self.name = name
123124
self.runas_name = runas_name(name)
@@ -139,3 +140,4 @@ def __init__(
139140
int(healthcheck_start_period) if healthcheck_start_period else None
140141
)
141142
self.healthcheck_retries = healthcheck_retries
143+
self.user = user

tox_docker/plugin.py

+8
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ def docker_run(
9797
if not os.path.exists(source):
9898
raise ValueError(f"Volume source {source!r} does not exist")
9999

100+
user = None
101+
if container_config.user:
102+
try:
103+
user = int(container_config.user)
104+
except ValueError:
105+
user = container_config.user
106+
100107
log(f"run {container_config.image!r} (from {container_config.name!r})")
101108
container = docker.containers.run(
102109
str(container_config.image),
@@ -108,6 +115,7 @@ def docker_run(
108115
ports=ports,
109116
publish_all_ports=len(ports) == 0,
110117
mounts=container_config.mounts,
118+
user=user,
111119
)
112120
container.reload() # TODO: why do we need this?
113121
return container

tox_docker/tox3/config.py

+5
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ def parse_container_config(
133133
if reader.getstring("volumes"):
134134
volumes = [Volume(line) for line in reader.getlist("volumes")]
135135

136+
user = None
137+
if reader.getstring("user"):
138+
user = getstring(reader, "user")
139+
136140
return ContainerConfig(
137141
name=container_name,
138142
image=Image(reader.getstring("image")),
@@ -146,4 +150,5 @@ def parse_container_config(
146150
ports=ports,
147151
links=links,
148152
volumes=volumes,
153+
user=user
149154
)

tox_docker/tox4/config.py

+7
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ def register_config(self) -> None:
8686
default=0,
8787
desc="docker healthcheck retry count",
8888
)
89+
self.add_config(
90+
keys=["user"],
91+
of_type=str,
92+
default="",
93+
desc="Username or UID to run commands as inside the container",
94+
)
8995

9096

9197
def parse_container_config(docker_config: DockerConfigSet) -> ContainerConfig:
@@ -102,4 +108,5 @@ def parse_container_config(docker_config: DockerConfigSet) -> ContainerConfig:
102108
ports=docker_config["ports"],
103109
links=docker_config["links"],
104110
volumes=docker_config["volumes"],
111+
user=docker_config["user"]
105112
)

0 commit comments

Comments
 (0)