Skip to content

Commit 06838ff

Browse files
bfirshshin-
authored andcommitted
Fix volume path passed by run to create_container
Seems like this is pretty much ignored by Docker, so it wasn't causing any visible issues, except when a volume name was used instead of a path. Also, added integration tests. Ref #1380 Signed-off-by: Ben Firshman <[email protected]>
1 parent bed7d65 commit 06838ff

File tree

4 files changed

+47
-6
lines changed

4 files changed

+47
-6
lines changed

docker/api/container.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,10 @@ def create_container(self, image, command=None, hostname=None, user=None,
313313
314314
**Using volumes**
315315
316-
Volume declaration is done in two parts. Provide a list of mountpoints
317-
to the with the ``volumes`` parameter, and declare mappings in the
318-
``host_config`` section.
316+
Volume declaration is done in two parts. Provide a list of
317+
paths to use as mountpoints inside the container with the
318+
``volumes`` parameter, and declare mappings from paths on the host
319+
in the ``host_config`` section.
319320
320321
.. code-block:: python
321322
@@ -392,7 +393,8 @@ def create_container(self, image, command=None, hostname=None, user=None,
392393
version 1.10. Use ``host_config`` instead.
393394
dns_opt (:py:class:`list`): Additional options to be added to the
394395
container's ``resolv.conf`` file
395-
volumes (str or list):
396+
volumes (str or list): List of paths inside the container to use
397+
as volumes.
396398
volumes_from (:py:class:`list`): List of container names or Ids to
397399
get volumes from.
398400
network_disabled (bool): Disable networking

docker/models/containers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,5 +885,5 @@ def _create_container_args(kwargs):
885885
for p in sorted(port_bindings.keys())]
886886
binds = create_kwargs['host_config'].get('Binds')
887887
if binds:
888-
create_kwargs['volumes'] = [v.split(':')[0] for v in binds]
888+
create_kwargs['volumes'] = [v.split(':')[1] for v in binds]
889889
return create_kwargs

tests/integration/models_containers_test.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import docker
2+
import tempfile
23
from .base import BaseIntegrationTest, TEST_API_VERSION
34

45

@@ -32,6 +33,42 @@ def test_run_with_image_that_does_not_exist(self):
3233
with self.assertRaises(docker.errors.ImageNotFound):
3334
client.containers.run("dockerpytest_does_not_exist")
3435

36+
def test_run_with_volume(self):
37+
client = docker.from_env(version=TEST_API_VERSION)
38+
path = tempfile.mkdtemp()
39+
40+
container = client.containers.run(
41+
"alpine", "sh -c 'echo \"hello\" > /insidecontainer/test'",
42+
volumes=["%s:/insidecontainer" % path],
43+
detach=True
44+
)
45+
self.tmp_containers.append(container.id)
46+
container.wait()
47+
48+
out = client.containers.run(
49+
"alpine", "cat /insidecontainer/test",
50+
volumes=["%s:/insidecontainer" % path]
51+
)
52+
self.assertEqual(out, b'hello\n')
53+
54+
def test_run_with_named_volume(self):
55+
client = docker.from_env(version=TEST_API_VERSION)
56+
client.volumes.create(name="somevolume")
57+
58+
container = client.containers.run(
59+
"alpine", "sh -c 'echo \"hello\" > /insidecontainer/test'",
60+
volumes=["somevolume:/insidecontainer"],
61+
detach=True
62+
)
63+
self.tmp_containers.append(container.id)
64+
container.wait()
65+
66+
out = client.containers.run(
67+
"alpine", "cat /insidecontainer/test",
68+
volumes=["somevolume:/insidecontainer"]
69+
)
70+
self.assertEqual(out, b'hello\n')
71+
3572
def test_get(self):
3673
client = docker.from_env(version=TEST_API_VERSION)
3774
container = client.containers.run("alpine", "sleep 300", detach=True)

tests/unit/models_containers_test.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ def test_create_container_args(self):
100100
volumes=[
101101
'/home/user1/:/mnt/vol2',
102102
'/var/www:/mnt/vol1:ro',
103+
'volumename:/mnt/vol3',
103104
],
104105
volumes_from=['container'],
105106
working_dir='/code'
@@ -116,6 +117,7 @@ def test_create_container_args(self):
116117
'Binds': [
117118
'/home/user1/:/mnt/vol2',
118119
'/var/www:/mnt/vol1:ro',
120+
'volumename:/mnt/vol3',
119121
],
120122
'BlkioDeviceReadBps': [{'Path': 'foo', 'Rate': 3}],
121123
'BlkioDeviceReadIOps': [{'Path': 'foo', 'Rate': 3}],
@@ -181,7 +183,7 @@ def test_create_container_args(self):
181183
tty=True,
182184
user='bob',
183185
volume_driver='some_driver',
184-
volumes=['/home/user1/', '/var/www'],
186+
volumes=['/mnt/vol2', '/mnt/vol1', '/mnt/vol3'],
185187
working_dir='/code'
186188
)
187189

0 commit comments

Comments
 (0)