Skip to content

Commit dba1c45

Browse files
committed
Merge pull request #291 from yograterol/master
Volumes parameter on create_container should be validated as string
2 parents 15b22e7 + 23a57c2 commit dba1c45

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

docker/client.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ def _container_config(self, image, command, hostname=None, user=None,
117117
else:
118118
units = {'b': 1,
119119
'k': 1024,
120-
'm': 1024*1024,
121-
'g': 1024*1024*1024}
120+
'm': 1024 * 1024,
121+
'g': 1024 * 1024 * 1024}
122122
suffix = mem_limit[-1].lower()
123123

124124
# Check if the variable is a string representation of an int
@@ -158,6 +158,9 @@ def _container_config(self, image, command, hostname=None, user=None,
158158
exposed_ports['{0}/{1}'.format(port, proto)] = {}
159159
ports = exposed_ports
160160

161+
if isinstance(volumes, six.string_types):
162+
volumes = [volumes, ]
163+
161164
if isinstance(volumes, list):
162165
volumes_dict = {}
163166
for vol in volumes:
@@ -500,6 +503,9 @@ def create_container(self, image, command=None, hostname=None, user=None,
500503
cpu_shares=None, working_dir=None, domainname=None,
501504
memswap_limit=0):
502505

506+
if isinstance(volumes, six.string_types):
507+
volumes = [volumes, ]
508+
503509
config = self._container_config(
504510
image, command, hostname, user, detach, stdin_open, tty, mem_limit,
505511
ports, environment, dns, volumes, volumes_from, network_disabled,

tests/test.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,31 @@ def test_create_container_with_binds(self):
248248
self.assertEqual(args[1]['headers'],
249249
{'Content-Type': 'application/json'})
250250

251+
def test_create_container_with_volume_string(self):
252+
mount_dest = '/mnt'
253+
254+
try:
255+
self.client.create_container('busybox', ['ls', mount_dest],
256+
volumes=mount_dest)
257+
except Exception as e:
258+
self.fail('Command should not raise exception: {0}'.format(e))
259+
260+
args = fake_request.call_args
261+
self.assertEqual(args[0][0],
262+
url_prefix + 'containers/create')
263+
self.assertEqual(json.loads(args[1]['data']),
264+
json.loads('''
265+
{"Tty": false, "Image": "busybox",
266+
"Cmd": ["ls", "/mnt"], "AttachStdin": false,
267+
"Volumes": {"/mnt": {}}, "Memory": 0,
268+
"AttachStderr": true,
269+
"AttachStdout": true, "OpenStdin": false,
270+
"StdinOnce": false,
271+
"NetworkDisabled": false,
272+
"MemorySwap": 0}'''))
273+
self.assertEqual(args[1]['headers'],
274+
{'Content-Type': 'application/json'})
275+
251276
def test_create_container_with_ports(self):
252277
try:
253278
self.client.create_container('busybox', 'ls',

0 commit comments

Comments
 (0)