Skip to content

Commit

Permalink
add support for the --user option to docker run
Browse files Browse the repository at this point in the history
  • Loading branch information
Neile Havens committed Jun 17, 2022
1 parent 7b573c5 commit f13cfa7
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ The ``[docker:container-name]`` section may contain the following directives:
test run until the container reports healthy, and will fail the test
run if it never does so (within the parameters specified).

``user``
The `user<https://docs.docker.com/engine/reference/run/#user>`__ Username
or UID to run commands as inside the container.

Command-Line Arguments
----------------------

Expand Down Expand Up @@ -182,6 +186,8 @@ Example
# testing use cases, as this could persist data between test runs
volumes =
bind:rw:/my/own/datadir:/var/lib/postgresql/data
# Run the container under the specified user
user = 1234
[docker:appserv]
# You can use any value that `docker run` would accept as the image
Expand Down
2 changes: 2 additions & 0 deletions tox_docker/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ def __init__(
ports: Optional[Collection[Port]] = None,
links: Optional[Collection[Link]] = None,
volumes: Optional[Collection[Volume]] = None,
user: Optional[str] = None,
) -> None:
self.name = name
self.runas_name = runas_name(name)
Expand All @@ -139,3 +140,4 @@ def __init__(
int(healthcheck_start_period) if healthcheck_start_period else None
)
self.healthcheck_retries = healthcheck_retries
self.user = user
8 changes: 8 additions & 0 deletions tox_docker/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ def docker_run(
if not os.path.exists(source):
raise ValueError(f"Volume source {source!r} does not exist")

user = None
if container_config.user:
try:
user = int(container_config.user)
except ValueError:
user = container_config.user

log(f"run {container_config.image!r} (from {container_config.name!r})")
container = docker.containers.run(
str(container_config.image),
Expand All @@ -108,6 +115,7 @@ def docker_run(
ports=ports,
publish_all_ports=len(ports) == 0,
mounts=container_config.mounts,
user=user,
)
container.reload() # TODO: why do we need this?
return container
Expand Down
5 changes: 5 additions & 0 deletions tox_docker/tox3/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ def parse_container_config(
if reader.getstring("volumes"):
volumes = [Volume(line) for line in reader.getlist("volumes")]

user = None
if reader.getstring("user"):
user = getstring(reader, "user")

return ContainerConfig(
name=container_name,
image=Image(reader.getstring("image")),
Expand All @@ -146,4 +150,5 @@ def parse_container_config(
ports=ports,
links=links,
volumes=volumes,
user=user
)
7 changes: 7 additions & 0 deletions tox_docker/tox4/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ def register_config(self) -> None:
default=0,
desc="docker healthcheck retry count",
)
self.add_config(
keys=["user"],
of_type=str,
default="",
desc="Username or UID to run commands as inside the container",
)


def parse_container_config(docker_config: DockerConfigSet) -> ContainerConfig:
Expand All @@ -102,4 +108,5 @@ def parse_container_config(docker_config: DockerConfigSet) -> ContainerConfig:
ports=docker_config["ports"],
links=docker_config["links"],
volumes=docker_config["volumes"],
user=docker_config["user"]
)

0 comments on commit f13cfa7

Please sign in to comment.