Răsfoiți Sursa

Allow setting a one-off container name

Signed-off-by: Daniel Nephin <[email protected]>
Daniel Nephin 10 ani în urmă
părinte
comite
d2718bed99

+ 4 - 0
compose/cli/main.py

@@ -308,6 +308,7 @@ class TopLevelCommand(Command):
             --allow-insecure-ssl  Deprecated - no effect.
             -d                    Detached mode: Run container in the background, print
                                   new container name.
+            --name NAME           Assign a name to the container
             --entrypoint CMD      Override the entrypoint of the image.
             -e KEY=VAL            Set an environment variable (can be used multiple times)
             -u, --user=""         Run as specified username or uid
@@ -374,6 +375,9 @@ class TopLevelCommand(Command):
                 'can not be used togather'
             )
 
+        if options['--name']:
+            container_options['name'] = options['--name']
+
         try:
             container = service.create_container(
                 quiet=True,

+ 1 - 1
compose/service.py

@@ -586,7 +586,7 @@ class Service(object):
 
         if self.custom_container_name() and not one_off:
             container_options['name'] = self.custom_container_name()
-        else:
+        elif not container_options.get('name'):
             container_options['name'] = self.get_container_name(number, one_off)
 
         if add_config_hash:

+ 10 - 0
tests/integration/cli_test.py

@@ -391,6 +391,16 @@ class CLITestCase(DockerClientTestCase):
         self.assertEqual(port_short, "127.0.0.1:30000")
         self.assertEqual(port_full, "127.0.0.1:30001")
 
+    @mock.patch('dockerpty.start')
+    def test_run_with_custom_name(self, _):
+        self.command.base_dir = 'tests/fixtures/environment-composefile'
+        name = 'the-container-name'
+        self.command.dispatch(['run', '--name', name, 'service'], None)
+
+        service = self.project.get_service('service')
+        container, = service.containers(stopped=True, one_off=True)
+        self.assertEqual(container.name, name)
+
     def test_rm(self):
         service = self.project.get_service('simple')
         service.create_container()

+ 4 - 0
tests/unit/cli_test.py

@@ -112,6 +112,7 @@ class CLITestCase(unittest.TestCase):
             '--service-ports': None,
             '--publish': [],
             '--rm': None,
+            '--name': None,
         })
 
         _, _, call_kwargs = mock_client.create_container.mock_calls[0]
@@ -141,6 +142,7 @@ class CLITestCase(unittest.TestCase):
             '--service-ports': None,
             '--publish': [],
             '--rm': None,
+            '--name': None,
         })
         _, _, call_kwargs = mock_client.create_container.mock_calls[0]
         self.assertEquals(call_kwargs['host_config']['RestartPolicy']['Name'], 'always')
@@ -166,6 +168,7 @@ class CLITestCase(unittest.TestCase):
             '--service-ports': None,
             '--publish': [],
             '--rm': True,
+            '--name': None,
         })
         _, _, call_kwargs = mock_client.create_container.mock_calls[0]
         self.assertFalse('RestartPolicy' in call_kwargs['host_config'])
@@ -195,4 +198,5 @@ class CLITestCase(unittest.TestCase):
                 '--service-ports': True,
                 '--publish': ['80:80'],
                 '--rm': None,
+                '--name': None,
             })

+ 13 - 0
tests/unit/service_test.py

@@ -150,6 +150,19 @@ class ServiceTest(unittest.TestCase):
         self.assertEqual(opts['hostname'], 'name.sub', 'hostname')
         self.assertEqual(opts['domainname'], 'domain.tld', 'domainname')
 
+    def test_get_container_create_options_with_name_option(self):
+        service = Service(
+            'foo',
+            image='foo',
+            client=self.mock_client,
+            container_name='foo1')
+        name = 'the_new_name'
+        opts = service._get_container_create_options(
+            {'name': name},
+            1,
+            one_off=True)
+        self.assertEqual(opts['name'], name)
+
     def test_get_container_not_found(self):
         self.mock_client.containers.return_value = []
         service = Service('foo', client=self.mock_client, image='foo')