瀏覽代碼

enable -v flag for docker-compose run command

Give user ability to attach volumes while running containers with
docker-compose run command. Example is given in the test implementation,
command is compatible with the one provided by docker engine.

Signed-off-by: Piotr Szymanski <[email protected]>
Piotr Szymanski 9 年之前
父節點
當前提交
83388ec31a

+ 7 - 1
compose/cli/main.py

@@ -24,6 +24,7 @@ from ..config import ConfigurationError
 from ..config import parse_environment
 from ..config.environment import Environment
 from ..config.serialize import serialize_config
+from ..config.types import VolumeSpec
 from ..const import IS_WINDOWS_PLATFORM
 from ..errors import StreamParseError
 from ..progress_stream import StreamOutputError
@@ -678,7 +679,7 @@ class TopLevelCommand(object):
         running. If you do not want to start linked services, use
         `docker-compose run --no-deps SERVICE COMMAND [ARGS...]`.
 
-        Usage: run [options] [-p PORT...] [-e KEY=VAL...] SERVICE [COMMAND] [ARGS...]
+        Usage: run [options] [-v VOLUME...] [-p PORT...] [-e KEY=VAL...] SERVICE [COMMAND] [ARGS...]
 
         Options:
             -d                    Detached mode: Run container in the background, print
@@ -692,6 +693,7 @@ class TopLevelCommand(object):
             -p, --publish=[]      Publish a container's port(s) to the host
             --service-ports       Run command with the service's ports enabled and mapped
                                   to the host.
+            -v, --volume=[]       Bind mount a volume (default [])
             -T                    Disable pseudo-tty allocation. By default `docker-compose run`
                                   allocates a TTY.
             -w, --workdir=""      Working directory inside the container
@@ -1035,6 +1037,10 @@ def build_container_options(options, detach, command):
     if options['--workdir']:
         container_options['working_dir'] = options['--workdir']
 
+    if options['--volume']:
+        volumes = [VolumeSpec.parse(i) for i in options['--volume']]
+        container_options['volumes'] = volumes
+
     return container_options
 
 

+ 34 - 0
tests/acceptance/cli_test.py

@@ -5,6 +5,7 @@ from __future__ import unicode_literals
 import datetime
 import json
 import os
+import os.path
 import signal
 import subprocess
 import time
@@ -557,6 +558,39 @@ class CLITestCase(DockerClientTestCase):
 
         self.assertEqual(old_ids, new_ids)
 
+    def test_run_one_off_with_volume(self):
+        self.base_dir = 'tests/fixtures/simple-composefile-volume-ready'
+        volume_path = os.path.abspath(os.path.join(os.getcwd(), self.base_dir, 'files'))
+        cmd_result = self.dispatch([
+            'run',
+            '-v', '{}:/data'.format(volume_path),
+            'simple',
+            'cat', '/data/example.txt'
+        ])
+        assert cmd_result.stdout.strip() == 'FILE_CONTENT'
+
+    def test_run_one_off_with_multiple_volumes(self):
+        self.base_dir = 'tests/fixtures/simple-composefile-volume-ready'
+        volume_path = os.path.abspath(os.path.join(os.getcwd(), self.base_dir, 'files'))
+
+        cmd_result = self.dispatch([
+            'run',
+            '-v', '{}:/data'.format(volume_path),
+            '-v', '{}:/data1'.format(volume_path),
+            'simple',
+            'cat', '/data/example.txt'
+        ])
+        assert cmd_result.stdout.strip() == 'FILE_CONTENT'
+
+        cmd_result = self.dispatch([
+            'run',
+            '-v', '{}:/data'.format(volume_path),
+            '-v', '{}:/data1'.format(volume_path),
+            'simple',
+            'cat', '/data1/example.txt'
+        ])
+        assert cmd_result.stdout.strip() == 'FILE_CONTENT'
+
     def test_create_with_force_recreate_and_no_recreate(self):
         self.dispatch(
             ['create', '--force-recreate', '--no-recreate'],

+ 2 - 0
tests/fixtures/simple-composefile-volume-ready/docker-compose.yml

@@ -0,0 +1,2 @@
+simple:
+  image: busybox:latest

+ 1 - 0
tests/fixtures/simple-composefile-volume-ready/files/example.txt

@@ -0,0 +1 @@
+FILE_CONTENT

+ 3 - 0
tests/unit/cli_test.py

@@ -119,6 +119,7 @@ class CLITestCase(unittest.TestCase):
                 '--entrypoint': None,
                 '--service-ports': None,
                 '--publish': [],
+                '--volume': [],
                 '--rm': None,
                 '--name': None,
                 '--workdir': None,
@@ -153,6 +154,7 @@ class CLITestCase(unittest.TestCase):
             '--entrypoint': None,
             '--service-ports': None,
             '--publish': [],
+            '--volume': [],
             '--rm': None,
             '--name': None,
             '--workdir': None,
@@ -175,6 +177,7 @@ class CLITestCase(unittest.TestCase):
             '--entrypoint': None,
             '--service-ports': None,
             '--publish': [],
+            '--volume': [],
             '--rm': True,
             '--name': None,
             '--workdir': None,