mirror of https://github.com/docker/compose.git
Merge pull request #5725 from docker/geigerzaehler-run-with-network-alias
Add --use-aliases flag to run command
This commit is contained in:
commit
4ceeaad089
|
@ -803,6 +803,8 @@ class TopLevelCommand(object):
|
||||||
-p, --publish=[] Publish a container's port(s) to the host
|
-p, --publish=[] Publish a container's port(s) to the host
|
||||||
--service-ports Run command with the service's ports enabled and mapped
|
--service-ports Run command with the service's ports enabled and mapped
|
||||||
to the host.
|
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 [])
|
-v, --volume=[] Bind mount a volume (default [])
|
||||||
-T Disable pseudo-tty allocation. By default `docker-compose run`
|
-T Disable pseudo-tty allocation. By default `docker-compose run`
|
||||||
allocates a TTY.
|
allocates a TTY.
|
||||||
|
@ -1281,8 +1283,10 @@ def run_one_off_container(container_options, project, service, options, toplevel
|
||||||
one_off=True,
|
one_off=True,
|
||||||
**container_options)
|
**container_options)
|
||||||
|
|
||||||
|
use_network_aliases = options['--use-aliases']
|
||||||
|
|
||||||
if options.get('--detach'):
|
if options.get('--detach'):
|
||||||
service.start_container(container)
|
service.start_container(container, use_network_aliases)
|
||||||
print(container.name)
|
print(container.name)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -1298,7 +1302,7 @@ def run_one_off_container(container_options, project, service, options, toplevel
|
||||||
try:
|
try:
|
||||||
try:
|
try:
|
||||||
if IS_WINDOWS_PLATFORM or use_cli:
|
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(
|
exit_code = call_docker(
|
||||||
["start", "--attach", "--interactive", container.id],
|
["start", "--attach", "--interactive", container.id],
|
||||||
toplevel_options
|
toplevel_options
|
||||||
|
@ -1312,7 +1316,7 @@ def run_one_off_container(container_options, project, service, options, toplevel
|
||||||
)
|
)
|
||||||
pty = PseudoTerminal(project.client, operation)
|
pty = PseudoTerminal(project.client, operation)
|
||||||
sockets = pty.sockets()
|
sockets = pty.sockets()
|
||||||
service.start_container(container)
|
service.start_container(container, use_network_aliases)
|
||||||
pty.start(sockets)
|
pty.start(sockets)
|
||||||
exit_code = container.wait()
|
exit_code = container.wait()
|
||||||
except (signals.ShutdownException):
|
except (signals.ShutdownException):
|
||||||
|
|
|
@ -557,8 +557,8 @@ class Service(object):
|
||||||
container.attach_log_stream()
|
container.attach_log_stream()
|
||||||
return self.start_container(container)
|
return self.start_container(container)
|
||||||
|
|
||||||
def start_container(self, container):
|
def start_container(self, container, use_network_aliases=True):
|
||||||
self.connect_container_to_networks(container)
|
self.connect_container_to_networks(container, use_network_aliases)
|
||||||
try:
|
try:
|
||||||
container.start()
|
container.start()
|
||||||
except APIError as ex:
|
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')
|
connected_networks = container.get('NetworkSettings.Networks')
|
||||||
|
|
||||||
for network, netdefs in self.prioritized_networks.items():
|
for network, netdefs in self.prioritized_networks.items():
|
||||||
|
@ -583,10 +583,11 @@ class Service(object):
|
||||||
continue
|
continue
|
||||||
self.client.disconnect_container_from_network(container.id, network)
|
self.client.disconnect_container_from_network(container.id, network)
|
||||||
|
|
||||||
log.debug('Connecting to {}'.format(network))
|
aliases = self._get_aliases(netdefs, container) if use_network_aliases else []
|
||||||
|
|
||||||
self.client.connect_container_to_network(
|
self.client.connect_container_to_network(
|
||||||
container.id, network,
|
container.id, network,
|
||||||
aliases=self._get_aliases(netdefs, container),
|
aliases=aliases,
|
||||||
ipv4_address=netdefs.get('ipv4_address', None),
|
ipv4_address=netdefs.get('ipv4_address', None),
|
||||||
ipv6_address=netdefs.get('ipv6_address', None),
|
ipv6_address=netdefs.get('ipv6_address', None),
|
||||||
links=self._get_links(False),
|
links=self._get_links(False),
|
||||||
|
@ -692,9 +693,6 @@ class Service(object):
|
||||||
return 1 if not numbers else max(numbers) + 1
|
return 1 if not numbers else max(numbers) + 1
|
||||||
|
|
||||||
def _get_aliases(self, network, container=None):
|
def _get_aliases(self, network, container=None):
|
||||||
if container and container.labels.get(LABEL_ONE_OFF) == "True":
|
|
||||||
return []
|
|
||||||
|
|
||||||
return list(
|
return list(
|
||||||
{self.name} |
|
{self.name} |
|
||||||
({container.short_id} if container else set()) |
|
({container.short_id} if container else set()) |
|
||||||
|
|
|
@ -1915,6 +1915,28 @@ class CLITestCase(DockerClientTestCase):
|
||||||
container = service.containers(stopped=True, one_off=True)[0]
|
container = service.containers(stopped=True, one_off=True)[0]
|
||||||
assert workdir == container.get('Config.WorkingDir')
|
assert workdir == container.get('Config.WorkingDir')
|
||||||
|
|
||||||
|
@v2_only()
|
||||||
|
def test_run_service_with_use_aliases(self):
|
||||||
|
filename = 'network-aliases.yml'
|
||||||
|
self.base_dir = 'tests/fixtures/networks'
|
||||||
|
self.dispatch(['-f', filename, 'run', '-d', '--use-aliases', 'web', 'top'])
|
||||||
|
|
||||||
|
back_name = '{}_back'.format(self.project.name)
|
||||||
|
front_name = '{}_front'.format(self.project.name)
|
||||||
|
|
||||||
|
web_container = self.project.get_service('web').containers(one_off=OneOffFilter.only)[0]
|
||||||
|
|
||||||
|
back_aliases = web_container.get(
|
||||||
|
'NetworkSettings.Networks.{}.Aliases'.format(back_name)
|
||||||
|
)
|
||||||
|
assert 'web' in back_aliases
|
||||||
|
front_aliases = web_container.get(
|
||||||
|
'NetworkSettings.Networks.{}.Aliases'.format(front_name)
|
||||||
|
)
|
||||||
|
assert 'web' in front_aliases
|
||||||
|
assert 'forward_facing' in front_aliases
|
||||||
|
assert 'ahead' in front_aliases
|
||||||
|
|
||||||
@v2_only()
|
@v2_only()
|
||||||
def test_run_interactive_connects_to_network(self):
|
def test_run_interactive_connects_to_network(self):
|
||||||
self.base_dir = 'tests/fixtures/networks'
|
self.base_dir = 'tests/fixtures/networks'
|
||||||
|
|
|
@ -124,6 +124,7 @@ class CLITestCase(unittest.TestCase):
|
||||||
'-T': None,
|
'-T': None,
|
||||||
'--entrypoint': None,
|
'--entrypoint': None,
|
||||||
'--service-ports': None,
|
'--service-ports': None,
|
||||||
|
'--use-aliases': None,
|
||||||
'--publish': [],
|
'--publish': [],
|
||||||
'--volume': [],
|
'--volume': [],
|
||||||
'--rm': None,
|
'--rm': None,
|
||||||
|
@ -162,6 +163,7 @@ class CLITestCase(unittest.TestCase):
|
||||||
'-T': None,
|
'-T': None,
|
||||||
'--entrypoint': None,
|
'--entrypoint': None,
|
||||||
'--service-ports': None,
|
'--service-ports': None,
|
||||||
|
'--use-aliases': None,
|
||||||
'--publish': [],
|
'--publish': [],
|
||||||
'--volume': [],
|
'--volume': [],
|
||||||
'--rm': None,
|
'--rm': None,
|
||||||
|
@ -183,6 +185,7 @@ class CLITestCase(unittest.TestCase):
|
||||||
'-T': None,
|
'-T': None,
|
||||||
'--entrypoint': None,
|
'--entrypoint': None,
|
||||||
'--service-ports': None,
|
'--service-ports': None,
|
||||||
|
'--use-aliases': None,
|
||||||
'--publish': [],
|
'--publish': [],
|
||||||
'--volume': [],
|
'--volume': [],
|
||||||
'--rm': True,
|
'--rm': True,
|
||||||
|
@ -214,6 +217,7 @@ class CLITestCase(unittest.TestCase):
|
||||||
'-T': None,
|
'-T': None,
|
||||||
'--entrypoint': None,
|
'--entrypoint': None,
|
||||||
'--service-ports': True,
|
'--service-ports': True,
|
||||||
|
'--use-aliases': None,
|
||||||
'--publish': ['80:80'],
|
'--publish': ['80:80'],
|
||||||
'--rm': None,
|
'--rm': None,
|
||||||
'--name': None,
|
'--name': None,
|
||||||
|
|
Loading…
Reference in New Issue