Skip to content

Commit 63df0b9

Browse files
committed
Merge branch 'master' of github.com:docker/docker-py
2 parents 0282e12 + db1a93f commit 63df0b9

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

docker/client.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,19 +301,23 @@ def events(self, since=None, until=None, filters=None, decode=None):
301301

302302
@check_resource
303303
def exec_create(self, container, cmd, stdout=True, stderr=True, tty=False,
304-
privileged=False):
304+
privileged=False, user=''):
305305
if utils.compare_version('1.15', self._version) < 0:
306306
raise errors.InvalidVersion('Exec is not supported in API < 1.15')
307307
if privileged and utils.compare_version('1.19', self._version) < 0:
308308
raise errors.InvalidVersion(
309309
'Privileged exec is not supported in API < 1.19'
310310
)
311+
if user and utils.compare_version('1.19', self._version) < 0:
312+
raise errors.InvalidVersion(
313+
'User-specific exec is not supported in API < 1.19'
314+
)
311315
if isinstance(cmd, six.string_types):
312316
cmd = shlex.split(str(cmd))
313317

314318
data = {
315319
'Container': container,
316-
'User': '',
320+
'User': user,
317321
'Privileged': privileged,
318322
'Tty': tty,
319323
'AttachStdin': False,

docs/api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ Sets up an exec instance in a running container.
303303
* stdout (bool): Attach to stdout of the exec command if true. Default: True
304304
* stderr (bool): Attach to stderr of the exec command if true. Default: True
305305
* tty (bool): Allocate a pseudo-TTY. Default: False
306+
* user (str): User to execute command as. Default: root
306307

307308
**Returns** (dict): A dictionary with an exec 'Id' key.
308309

tests/integration_test.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,40 @@ def runTest(self):
895895
self.assertEqual(exec_log, expected)
896896

897897

898+
@unittest.skipIf(not EXEC_DRIVER_IS_NATIVE, 'Exec driver not native')
899+
class TestExecuteCommandStringAsUser(BaseTestCase):
900+
def runTest(self):
901+
container = self.client.create_container('busybox', 'cat',
902+
detach=True, stdin_open=True)
903+
id = container['Id']
904+
self.client.start(id)
905+
self.tmp_containers.append(id)
906+
907+
res = self.client.exec_create(id, 'whoami', user='default')
908+
self.assertIn('Id', res)
909+
910+
exec_log = self.client.exec_start(res)
911+
expected = b'default' if six.PY3 else 'default\n'
912+
self.assertEqual(exec_log, expected)
913+
914+
915+
@unittest.skipIf(not EXEC_DRIVER_IS_NATIVE, 'Exec driver not native')
916+
class TestExecuteCommandStringAsRoot(BaseTestCase):
917+
def runTest(self):
918+
container = self.client.create_container('busybox', 'cat',
919+
detach=True, stdin_open=True)
920+
id = container['Id']
921+
self.client.start(id)
922+
self.tmp_containers.append(id)
923+
924+
res = self.client.exec_create(id, 'whoami')
925+
self.assertIn('Id', res)
926+
927+
exec_log = self.client.exec_start(res)
928+
expected = b'root' if six.PY3 else 'root\n'
929+
self.assertEqual(exec_log, expected)
930+
931+
898932
@unittest.skipIf(not EXEC_DRIVER_IS_NATIVE, 'Exec driver not native')
899933
class TestExecuteCommandStreaming(BaseTestCase):
900934
def runTest(self):

0 commit comments

Comments
 (0)