summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoffrey F <f.joffrey@gmail.com>2015-08-26 15:36:47 -0700
committerJoffrey F <f.joffrey@gmail.com>2015-08-26 15:36:47 -0700
commitdb1a93fd27a7627300c1fd54040d2f727ada78a4 (patch)
treeddefddc8e0538dcdbd93ca3531f75092e4047c8c
parentddf879cc4ca58a4140dfa0f9798eba21e74c22a5 (diff)
parent1c1d7eee5a7a583635892a2ba753a67c82a80325 (diff)
downloaddocker-py-db1a93fd27a7627300c1fd54040d2f727ada78a4.tar.gz
Merge pull request #662 from stevenewey/exec_create_user
Exec create user
-rw-r--r--docker/client.py8
-rw-r--r--docs/api.md1
-rw-r--r--tests/integration_test.py34
3 files changed, 41 insertions, 2 deletions
diff --git a/docker/client.py b/docker/client.py
index 65e415e..b1f72e9 100644
--- a/docker/client.py
+++ b/docker/client.py
@@ -301,19 +301,23 @@ class Client(clientbase.ClientBase):
@check_resource
def exec_create(self, container, cmd, stdout=True, stderr=True, tty=False,
- privileged=False):
+ privileged=False, user=''):
if utils.compare_version('1.15', self._version) < 0:
raise errors.InvalidVersion('Exec is not supported in API < 1.15')
if privileged and utils.compare_version('1.19', self._version) < 0:
raise errors.InvalidVersion(
'Privileged exec is not supported in API < 1.19'
)
+ if user and utils.compare_version('1.19', self._version) < 0:
+ raise errors.InvalidVersion(
+ 'User-specific exec is not supported in API < 1.19'
+ )
if isinstance(cmd, six.string_types):
cmd = shlex.split(str(cmd))
data = {
'Container': container,
- 'User': '',
+ 'User': user,
'Privileged': privileged,
'Tty': tty,
'AttachStdin': False,
diff --git a/docs/api.md b/docs/api.md
index 4307ea0..607cd47 100644
--- a/docs/api.md
+++ b/docs/api.md
@@ -303,6 +303,7 @@ Sets up an exec instance in a running container.
* stdout (bool): Attach to stdout of the exec command if true. Default: True
* stderr (bool): Attach to stderr of the exec command if true. Default: True
* tty (bool): Allocate a pseudo-TTY. Default: False
+* user (str): User to execute command as. Default: root
**Returns** (dict): A dictionary with an exec 'Id' key.
diff --git a/tests/integration_test.py b/tests/integration_test.py
index 2de4a7f..6ba9c19 100644
--- a/tests/integration_test.py
+++ b/tests/integration_test.py
@@ -896,6 +896,40 @@ class TestExecuteCommandString(BaseTestCase):
@unittest.skipIf(not EXEC_DRIVER_IS_NATIVE, 'Exec driver not native')
+class TestExecuteCommandStringAsUser(BaseTestCase):
+ def runTest(self):
+ container = self.client.create_container('busybox', 'cat',
+ detach=True, stdin_open=True)
+ id = container['Id']
+ self.client.start(id)
+ self.tmp_containers.append(id)
+
+ res = self.client.exec_create(id, 'whoami', user='default')
+ self.assertIn('Id', res)
+
+ exec_log = self.client.exec_start(res)
+ expected = b'default' if six.PY3 else 'default\n'
+ self.assertEqual(exec_log, expected)
+
+
+@unittest.skipIf(not EXEC_DRIVER_IS_NATIVE, 'Exec driver not native')
+class TestExecuteCommandStringAsRoot(BaseTestCase):
+ def runTest(self):
+ container = self.client.create_container('busybox', 'cat',
+ detach=True, stdin_open=True)
+ id = container['Id']
+ self.client.start(id)
+ self.tmp_containers.append(id)
+
+ res = self.client.exec_create(id, 'whoami')
+ self.assertIn('Id', res)
+
+ exec_log = self.client.exec_start(res)
+ expected = b'root' if six.PY3 else 'root\n'
+ self.assertEqual(exec_log, expected)
+
+
+@unittest.skipIf(not EXEC_DRIVER_IS_NATIVE, 'Exec driver not native')
class TestExecuteCommandStreaming(BaseTestCase):
def runTest(self):
container = self.client.create_container('busybox', 'cat',