Kaydet (Commit) 06838fff authored tarafından Ben Firshman's avatar Ben Firshman Kaydeden (comit) Joffrey F

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: 's avatarBen Firshman <ben@firshman.co.uk>
üst bed7d65c
......@@ -313,9 +313,10 @@ class ContainerApiMixin(object):
**Using volumes**
Volume declaration is done in two parts. Provide a list of mountpoints
to the with the ``volumes`` parameter, and declare mappings in the
``host_config`` section.
Volume declaration is done in two parts. Provide a list of
paths to use as mountpoints inside the container with the
``volumes`` parameter, and declare mappings from paths on the host
in the ``host_config`` section.
.. code-block:: python
......@@ -392,7 +393,8 @@ class ContainerApiMixin(object):
version 1.10. Use ``host_config`` instead.
dns_opt (:py:class:`list`): Additional options to be added to the
container's ``resolv.conf`` file
volumes (str or list):
volumes (str or list): List of paths inside the container to use
as volumes.
volumes_from (:py:class:`list`): List of container names or Ids to
get volumes from.
network_disabled (bool): Disable networking
......
......@@ -885,5 +885,5 @@ def _create_container_args(kwargs):
for p in sorted(port_bindings.keys())]
binds = create_kwargs['host_config'].get('Binds')
if binds:
create_kwargs['volumes'] = [v.split(':')[0] for v in binds]
create_kwargs['volumes'] = [v.split(':')[1] for v in binds]
return create_kwargs
import docker
import tempfile
from .base import BaseIntegrationTest, TEST_API_VERSION
......@@ -32,6 +33,42 @@ class ContainerCollectionTest(BaseIntegrationTest):
with self.assertRaises(docker.errors.ImageNotFound):
client.containers.run("dockerpytest_does_not_exist")
def test_run_with_volume(self):
client = docker.from_env(version=TEST_API_VERSION)
path = tempfile.mkdtemp()
container = client.containers.run(
"alpine", "sh -c 'echo \"hello\" > /insidecontainer/test'",
volumes=["%s:/insidecontainer" % path],
detach=True
)
self.tmp_containers.append(container.id)
container.wait()
out = client.containers.run(
"alpine", "cat /insidecontainer/test",
volumes=["%s:/insidecontainer" % path]
)
self.assertEqual(out, b'hello\n')
def test_run_with_named_volume(self):
client = docker.from_env(version=TEST_API_VERSION)
client.volumes.create(name="somevolume")
container = client.containers.run(
"alpine", "sh -c 'echo \"hello\" > /insidecontainer/test'",
volumes=["somevolume:/insidecontainer"],
detach=True
)
self.tmp_containers.append(container.id)
container.wait()
out = client.containers.run(
"alpine", "cat /insidecontainer/test",
volumes=["somevolume:/insidecontainer"]
)
self.assertEqual(out, b'hello\n')
def test_get(self):
client = docker.from_env(version=TEST_API_VERSION)
container = client.containers.run("alpine", "sleep 300", detach=True)
......
......@@ -100,6 +100,7 @@ class ContainerCollectionTest(unittest.TestCase):
volumes=[
'/home/user1/:/mnt/vol2',
'/var/www:/mnt/vol1:ro',
'volumename:/mnt/vol3',
],
volumes_from=['container'],
working_dir='/code'
......@@ -116,6 +117,7 @@ class ContainerCollectionTest(unittest.TestCase):
'Binds': [
'/home/user1/:/mnt/vol2',
'/var/www:/mnt/vol1:ro',
'volumename:/mnt/vol3',
],
'BlkioDeviceReadBps': [{'Path': 'foo', 'Rate': 3}],
'BlkioDeviceReadIOps': [{'Path': 'foo', 'Rate': 3}],
......@@ -181,7 +183,7 @@ class ContainerCollectionTest(unittest.TestCase):
tty=True,
user='bob',
volume_driver='some_driver',
volumes=['/home/user1/', '/var/www'],
volumes=['/mnt/vol2', '/mnt/vol1', '/mnt/vol3'],
working_dir='/code'
)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment