diff --git a/README.rst b/README.rst index 64c9246..7144e11 100644 --- a/README.rst +++ b/README.rst @@ -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`__ Username + or UID to run commands as inside the container. + Command-Line Arguments ---------------------- @@ -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 diff --git a/tox_docker/config.py b/tox_docker/config.py index 1721a1d..a1b7e87 100644 --- a/tox_docker/config.py +++ b/tox_docker/config.py @@ -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) @@ -139,3 +140,4 @@ def __init__( int(healthcheck_start_period) if healthcheck_start_period else None ) self.healthcheck_retries = healthcheck_retries + self.user = user diff --git a/tox_docker/plugin.py b/tox_docker/plugin.py index eec57fd..d51c078 100644 --- a/tox_docker/plugin.py +++ b/tox_docker/plugin.py @@ -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), @@ -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 diff --git a/tox_docker/tox3/config.py b/tox_docker/tox3/config.py index eaa4850..6cb7a71 100644 --- a/tox_docker/tox3/config.py +++ b/tox_docker/tox3/config.py @@ -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")), @@ -146,4 +150,5 @@ def parse_container_config( ports=ports, links=links, volumes=volumes, + user=user ) diff --git a/tox_docker/tox4/config.py b/tox_docker/tox4/config.py index 8beb390..b3ad2d5 100644 --- a/tox_docker/tox4/config.py +++ b/tox_docker/tox4/config.py @@ -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: @@ -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"] )