Merge pull request #1406 from vdemeester/667-compose-port-scale

Fixing docker-compose port with scale (#667)
(cherry picked from commit 5b2a0cc73d)

Signed-off-by: Aanand Prasad <aanand.prasad@gmail.com>
This commit is contained in:
Aanand Prasad 2015-06-15 10:58:44 -07:00
parent 09018855ce
commit f353d9fbc0
3 changed files with 32 additions and 3 deletions

View File

@ -169,13 +169,14 @@ class TopLevelCommand(Command):
Usage: port [options] SERVICE PRIVATE_PORT
Options:
--protocol=proto tcp or udp (defaults to tcp)
--protocol=proto tcp or udp [default: tcp]
--index=index index of the container if there are multiple
instances of a service (defaults to 1)
instances of a service [default: 1]
"""
index = int(options.get('--index'))
service = project.get_service(options['SERVICE'])
try:
container = service.get_container(number=options.get('--index') or 1)
container = service.get_container(number=index)
except ValueError as e:
raise UserError(str(e))
print(container.get_local_port(

View File

@ -0,0 +1,6 @@
simple:
image: busybox:latest
command: /bin/sleep 300
ports:
- '3000'

View File

@ -1,4 +1,5 @@
from __future__ import absolute_import
from operator import attrgetter
import sys
import os
import shlex
@ -436,6 +437,27 @@ class CLITestCase(DockerClientTestCase):
self.assertEqual(get_port(3001), "0.0.0.0:49152")
self.assertEqual(get_port(3002), "")
def test_port_with_scale(self):
self.command.base_dir = 'tests/fixtures/ports-composefile-scale'
self.command.dispatch(['scale', 'simple=2'], None)
containers = sorted(
self.project.containers(service_names=['simple']),
key=attrgetter('name'))
@patch('sys.stdout', new_callable=StringIO)
def get_port(number, mock_stdout, index=None):
if index is None:
self.command.dispatch(['port', 'simple', str(number)], None)
else:
self.command.dispatch(['port', '--index=' + str(index), 'simple', str(number)], None)
return mock_stdout.getvalue().rstrip()
self.assertEqual(get_port(3000), containers[0].get_local_port(3000))
self.assertEqual(get_port(3000, index=1), containers[0].get_local_port(3000))
self.assertEqual(get_port(3000, index=2), containers[1].get_local_port(3000))
self.assertEqual(get_port(3002), "")
def test_env_file_relative_to_compose_file(self):
config_path = os.path.abspath('tests/fixtures/env-file/docker-compose.yml')
self.command.dispatch(['-f', config_path, 'up', '-d'], None)