mirror of https://github.com/docker/compose.git
Merge pull request #1711 from aanand/custom-names
Add container_name option for specifying a custom container name
This commit is contained in:
commit
cc0bfea8a1
|
@ -50,6 +50,7 @@ DOCKER_CONFIG_KEYS = [
|
|||
|
||||
ALLOWED_KEYS = DOCKER_CONFIG_KEYS + [
|
||||
'build',
|
||||
'container_name',
|
||||
'dockerfile',
|
||||
'expose',
|
||||
'external_links',
|
||||
|
|
|
@ -157,8 +157,14 @@ class Service(object):
|
|||
- starts containers until there are at least `desired_num` running
|
||||
- removes all stopped containers
|
||||
"""
|
||||
if not self.can_be_scaled():
|
||||
log.warn('Service %s specifies a port on the host. If multiple containers '
|
||||
if self.custom_container_name() and desired_num > 1:
|
||||
log.warn('The "%s" service is using the custom container name "%s". '
|
||||
'Docker requires each container to have a unique name. '
|
||||
'Remove the custom name to scale the service.'
|
||||
% (self.name, self.custom_container_name()))
|
||||
|
||||
if self.specifies_host_port():
|
||||
log.warn('The "%s" service specifies a port on the host. If multiple containers '
|
||||
'for this service are created on a single host, the port will clash.'
|
||||
% self.name)
|
||||
|
||||
|
@ -531,7 +537,8 @@ class Service(object):
|
|||
for k in DOCKER_CONFIG_KEYS if k in self.options)
|
||||
container_options.update(override_options)
|
||||
|
||||
container_options['name'] = self.get_container_name(number, one_off)
|
||||
container_options['name'] = self.custom_container_name() \
|
||||
or self.get_container_name(number, one_off)
|
||||
|
||||
if add_config_hash:
|
||||
config_hash = self.config_hash()
|
||||
|
@ -703,11 +710,14 @@ class Service(object):
|
|||
'{0}={1}'.format(LABEL_ONE_OFF, "True" if one_off else "False")
|
||||
]
|
||||
|
||||
def can_be_scaled(self):
|
||||
def custom_container_name(self):
|
||||
return self.options.get('container_name')
|
||||
|
||||
def specifies_host_port(self):
|
||||
for port in self.options.get('ports', []):
|
||||
if ':' in str(port):
|
||||
return False
|
||||
return True
|
||||
return True
|
||||
return False
|
||||
|
||||
def pull(self, insecure_registry=False):
|
||||
if 'image' not in self.options:
|
||||
|
|
10
docs/yml.md
10
docs/yml.md
|
@ -239,6 +239,16 @@ It's recommended that you use reverse-DNS notation to prevent your labels from c
|
|||
- "com.example.department=Finance"
|
||||
- "com.example.label-with-empty-value"
|
||||
|
||||
### container_name
|
||||
|
||||
Specify a custom container name, rather than a generated default name.
|
||||
|
||||
container_name: my-web-container
|
||||
|
||||
Because Docker container names must be unique, you cannot scale a service
|
||||
beyond 1 container if you have specified a custom name. Attempting to do so
|
||||
results in an error.
|
||||
|
||||
### log driver
|
||||
|
||||
Specify a logging driver for the service's containers, as with the ``--log-driver`` option for docker run ([documented here](http://docs.docker.com/reference/run/#logging-drivers-log-driver)).
|
||||
|
|
|
@ -699,6 +699,13 @@ class ServiceTest(DockerClientTestCase):
|
|||
for name in labels_list:
|
||||
self.assertIn((name, ''), labels)
|
||||
|
||||
def test_custom_container_name(self):
|
||||
service = self.create_service('web', container_name='my-web-container')
|
||||
self.assertEqual(service.custom_container_name(), 'my-web-container')
|
||||
|
||||
container = create_and_start_container(service)
|
||||
self.assertEqual(container.name, 'my-web-container')
|
||||
|
||||
def test_log_drive_invalid(self):
|
||||
service = self.create_service('web', log_driver='xxx')
|
||||
self.assertRaises(ValueError, lambda: create_and_start_container(service))
|
||||
|
|
Loading…
Reference in New Issue