'run' command can use network aliases for service

It is now possible for the 'run' command to use the network aliases
defined for the service.

Fixes #3492

Signed-off-by: Thomas Scholtes <thomas-scholtes@gmx.de>
This commit is contained in:
Thomas Scholtes 2017-05-10 10:36:05 +02:00 committed by Joffrey F
parent 9b2efd50f5
commit 5b6e02d13a
3 changed files with 23 additions and 17 deletions

View File

@ -803,6 +803,8 @@ 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.
--use-aliases Use the service's network aliases in the network(s) the
container connects to.
-v, --volume=[] Bind mount a volume (default [])
-T Disable pseudo-tty allocation. By default `docker-compose run`
allocates a TTY.
@ -1279,8 +1281,10 @@ def run_one_off_container(container_options, project, service, options, toplevel
one_off=True,
**container_options)
use_network_aliases = options['--use-aliases']
if options.get('--detach'):
service.start_container(container)
service.start_container(container, use_network_aliases)
print(container.name)
return
@ -1296,7 +1300,7 @@ def run_one_off_container(container_options, project, service, options, toplevel
try:
try:
if IS_WINDOWS_PLATFORM or use_cli:
service.connect_container_to_networks(container)
service.connect_container_to_networks(container, use_network_aliases)
exit_code = call_docker(
["start", "--attach", "--interactive", container.id],
toplevel_options
@ -1310,7 +1314,7 @@ def run_one_off_container(container_options, project, service, options, toplevel
)
pty = PseudoTerminal(project.client, operation)
sockets = pty.sockets()
service.start_container(container)
service.start_container(container, use_network_aliases)
pty.start(sockets)
exit_code = container.wait()
except (signals.ShutdownException):

View File

@ -557,8 +557,8 @@ class Service(object):
container.attach_log_stream()
return self.start_container(container)
def start_container(self, container):
self.connect_container_to_networks(container)
def start_container(self, container, use_network_aliases=True):
self.connect_container_to_networks(container, use_network_aliases)
try:
container.start()
except APIError as ex:
@ -574,7 +574,7 @@ class Service(object):
)
)
def connect_container_to_networks(self, container):
def connect_container_to_networks(self, container, use_network_aliases=True):
connected_networks = container.get('NetworkSettings.Networks')
for network, netdefs in self.prioritized_networks.items():
@ -583,10 +583,15 @@ class Service(object):
continue
self.client.disconnect_container_from_network(container.id, network)
log.debug('Connecting to {}'.format(network))
self.client.disconnect_container_from_network(
container.id,
network)
aliases = self._get_aliases(netdefs) if use_network_aliases else []
self.client.connect_container_to_network(
container.id, network,
aliases=self._get_aliases(netdefs, container),
aliases=aliases,
ipv4_address=netdefs.get('ipv4_address', None),
ipv6_address=netdefs.get('ipv6_address', None),
links=self._get_links(False),
@ -691,15 +696,8 @@ class Service(object):
numbers = [c.number for c in containers]
return 1 if not numbers else max(numbers) + 1
def _get_aliases(self, network, container=None):
if container and container.labels.get(LABEL_ONE_OFF) == "True":
return []
return list(
{self.name} |
({container.short_id} if container else set()) |
set(network.get('aliases', ()))
)
def _get_aliases(self, network):
return list({self.name} | set(network.get('aliases', ())))
def build_default_networking_config(self):
if not self.networks:

View File

@ -124,6 +124,7 @@ class CLITestCase(unittest.TestCase):
'-T': None,
'--entrypoint': None,
'--service-ports': None,
'--use-aliases': None,
'--publish': [],
'--volume': [],
'--rm': None,
@ -162,6 +163,7 @@ class CLITestCase(unittest.TestCase):
'-T': None,
'--entrypoint': None,
'--service-ports': None,
'--use-aliases': None,
'--publish': [],
'--volume': [],
'--rm': None,
@ -183,6 +185,7 @@ class CLITestCase(unittest.TestCase):
'-T': None,
'--entrypoint': None,
'--service-ports': None,
'--use-aliases': None,
'--publish': [],
'--volume': [],
'--rm': True,
@ -214,6 +217,7 @@ class CLITestCase(unittest.TestCase):
'-T': None,
'--entrypoint': None,
'--service-ports': True,
'--use-aliases': None,
'--publish': ['80:80'],
'--rm': None,
'--name': None,