瀏覽代碼

Provide user override option on command line

Allows overriding a user on the command line from the one specified in
the docker-compose.yml

The added tests verify that a specified user overrides a default
user in the docker-compose.yml file.

Based on commit f2f01e207b491866349db7168e3d48082d7abdda by @chmouel

Signed-off-by: Ian VanSchooten <[email protected]>
Ian VanSchooten 10 年之前
父節點
當前提交
86b723e227
共有 4 個文件被更改,包括 32 次插入0 次删除
  1. 5 0
      compose/cli/main.py
  2. 4 0
      tests/fixtures/user-composefile/docker-compose.yml
  3. 22 0
      tests/integration/cli_test.py
  4. 1 0
      tests/unit/cli_test.py

+ 5 - 0
compose/cli/main.py

@@ -275,6 +275,7 @@ class TopLevelCommand(Command):
                                   new container name.
             --entrypoint CMD      Override the entrypoint of the image.
             -e KEY=VAL            Set an environment variable (can be used multiple times)
+            -u, --user=""         Run as specified username or uid
             --no-deps             Don't start linked services.
             --rm                  Remove container after run. Ignored in detached mode.
             --service-ports       Run command with the service's ports enabled and mapped
@@ -322,6 +323,10 @@ class TopLevelCommand(Command):
 
         if options['--entrypoint']:
             container_options['entrypoint'] = options.get('--entrypoint')
+
+        if options['--user']:
+            container_options['user'] = options.get('--user')
+
         container = service.create_container(
             one_off=True,
             insecure_registry=insecure_registry,

+ 4 - 0
tests/fixtures/user-composefile/docker-compose.yml

@@ -0,0 +1,4 @@
+service:
+  image: busybox:latest
+  user: notauser
+  command: id

+ 22 - 0
tests/integration/cli_test.py

@@ -231,6 +231,28 @@ class CLITestCase(DockerClientTestCase):
             u'/bin/echo helloworld'
         )
 
+    @patch('dockerpty.start')
+    def test_run_service_with_user_overridden(self, _):
+        self.command.base_dir = 'tests/fixtures/user-composefile'
+        name = 'service'
+        user = 'sshd'
+        args = ['run', '--user={}'.format(user), name]
+        self.command.dispatch(args, None)
+        service = self.project.get_service(name)
+        container = service.containers(stopped=True, one_off=True)[0]
+        self.assertEqual(user, container.get('Config.User'))
+
+    @patch('dockerpty.start')
+    def test_run_service_with_user_overridden_short_form(self, _):
+        self.command.base_dir = 'tests/fixtures/user-composefile'
+        name = 'service'
+        user = 'sshd'
+        args = ['run', '-u', user, name]
+        self.command.dispatch(args, None)
+        service = self.project.get_service(name)
+        container = service.containers(stopped=True, one_off=True)[0]
+        self.assertEqual(user, container.get('Config.User'))
+
     @patch('dockerpty.start')
     def test_run_service_with_environement_overridden(self, _):
         name = 'service'

+ 1 - 0
tests/unit/cli_test.py

@@ -120,6 +120,7 @@ class CLITestCase(unittest.TestCase):
             'SERVICE': 'service',
             'COMMAND': None,
             '-e': ['BAR=NEW', 'OTHER=THREE'],
+            '--user': None,
             '--no-deps': None,
             '--allow-insecure-ssl': None,
             '-d': True,