@@ -8,7 +8,8 @@ class ExecApiMixin(object):
8
8
@utils .minimum_version ('1.15' )
9
9
@utils .check_resource
10
10
def exec_create (self , container , cmd , stdout = True , stderr = True ,
11
- stdin = False , tty = False , privileged = False , user = '' ):
11
+ stdin = False , tty = False , privileged = False , user = '' ,
12
+ environment = None ):
12
13
"""
13
14
Sets up an exec instance in a running container.
14
15
@@ -22,6 +23,9 @@ def exec_create(self, container, cmd, stdout=True, stderr=True,
22
23
tty (bool): Allocate a pseudo-TTY. Default: False
23
24
privileged (bool): Run as privileged.
24
25
user (str): User to execute command as. Default: root
26
+ environment (dict or list): A dictionary or a list of strings in
27
+ the following format ``["PASSWORD=xxx"]`` or
28
+ ``{"PASSWORD": "xxx"}``.
25
29
26
30
Returns:
27
31
(dict): A dictionary with an exec ``Id`` key.
@@ -31,17 +35,25 @@ def exec_create(self, container, cmd, stdout=True, stderr=True,
31
35
If the server returns an error.
32
36
"""
33
37
34
- if privileged and utils .compare_version ( '1.19' , self . _version ) < 0 :
38
+ if privileged and utils .version_lt ( self . _version , '1.19' ) :
35
39
raise errors .InvalidVersion (
36
40
'Privileged exec is not supported in API < 1.19'
37
41
)
38
- if user and utils .compare_version ( '1.19' , self . _version ) < 0 :
42
+ if user and utils .version_lt ( self . _version , '1.19' ) :
39
43
raise errors .InvalidVersion (
40
44
'User-specific exec is not supported in API < 1.19'
41
45
)
46
+ if environment is not None and utils .version_lt (self ._version , '1.25' ):
47
+ raise errors .InvalidVersion (
48
+ 'Setting environment for exec is not supported in API < 1.25'
49
+ )
50
+
42
51
if isinstance (cmd , six .string_types ):
43
52
cmd = utils .split_command (cmd )
44
53
54
+ if isinstance (environment , dict ):
55
+ environment = utils .utils .format_environment (environment )
56
+
45
57
data = {
46
58
'Container' : container ,
47
59
'User' : user ,
@@ -50,7 +62,8 @@ def exec_create(self, container, cmd, stdout=True, stderr=True,
50
62
'AttachStdin' : stdin ,
51
63
'AttachStdout' : stdout ,
52
64
'AttachStderr' : stderr ,
53
- 'Cmd' : cmd
65
+ 'Cmd' : cmd ,
66
+ 'Env' : environment ,
54
67
}
55
68
56
69
url = self ._url ('/containers/{0}/exec' , container )
@@ -97,6 +110,7 @@ def exec_resize(self, exec_id, height=None, width=None):
97
110
self ._raise_for_status (res )
98
111
99
112
@utils .minimum_version ('1.15' )
113
+ @utils .check_resource
100
114
def exec_start (self , exec_id , detach = False , tty = False , stream = False ,
101
115
socket = False ):
102
116
"""
@@ -118,8 +132,6 @@ def exec_start(self, exec_id, detach=False, tty=False, stream=False,
118
132
If the server returns an error.
119
133
"""
120
134
# we want opened socket if socket == True
121
- if isinstance (exec_id , dict ):
122
- exec_id = exec_id .get ('Id' )
123
135
124
136
data = {
125
137
'Tty' : tty ,
0 commit comments