From 6a3d1d06b5fc4eae7d09bf6c6511651281325a6d Mon Sep 17 00:00:00 2001 From: Aanand Prasad Date: Tue, 4 Mar 2014 17:58:42 +0000 Subject: [PATCH 1/4] Check NetworkSettings.Ports instead of HostConfig.PortBindings in tests The latter appears to be unreliable. --- tests/service_test.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/service_test.py b/tests/service_test.py index 63a31df5..458de676 100644 --- a/tests/service_test.py +++ b/tests/service_test.py @@ -209,25 +209,25 @@ class ServiceTest(DockerClientTestCase): def test_start_container_creates_ports(self): service = self.create_service('web', ports=[8000]) container = service.start_container().inspect() - self.assertEqual(list(container['HostConfig']['PortBindings'].keys()), ['8000/tcp']) - self.assertNotEqual(container['HostConfig']['PortBindings']['8000/tcp'][0]['HostPort'], '8000') + self.assertEqual(list(container['NetworkSettings']['Ports'].keys()), ['8000/tcp']) + self.assertNotEqual(container['NetworkSettings']['Ports']['8000/tcp'][0]['HostPort'], '8000') def test_start_container_creates_port_with_explicit_protocol(self): service = self.create_service('web', ports=['8000/udp']) container = service.start_container().inspect() - self.assertEqual(list(container['HostConfig']['PortBindings'].keys()), ['8000/udp']) + self.assertEqual(list(container['NetworkSettings']['Ports'].keys()), ['8000/udp']) def test_start_container_creates_fixed_external_ports(self): service = self.create_service('web', ports=['8000:8000']) container = service.start_container().inspect() - self.assertIn('8000/tcp', container['HostConfig']['PortBindings']) - self.assertEqual(container['HostConfig']['PortBindings']['8000/tcp'][0]['HostPort'], '8000') + self.assertIn('8000/tcp', container['NetworkSettings']['Ports']) + self.assertEqual(container['NetworkSettings']['Ports']['8000/tcp'][0]['HostPort'], '8000') def test_start_container_creates_fixed_external_ports_when_it_is_different_to_internal_port(self): service = self.create_service('web', ports=['8001:8000']) container = service.start_container().inspect() - self.assertIn('8000/tcp', container['HostConfig']['PortBindings']) - self.assertEqual(container['HostConfig']['PortBindings']['8000/tcp'][0]['HostPort'], '8001') + self.assertIn('8000/tcp', container['NetworkSettings']['Ports']) + self.assertEqual(container['NetworkSettings']['Ports']['8000/tcp'][0]['HostPort'], '8001') def test_scale(self): service = self.create_service('web') From 2d98071e552da0c79bcfb843f9b6bfd08ae8e6b7 Mon Sep 17 00:00:00 2001 From: Aanand Prasad Date: Tue, 4 Mar 2014 17:59:42 +0000 Subject: [PATCH 2/4] Support 'expose' config option, like docker's --expose Exposes ports to linked services without publishing them to the world. --- docs/yml.md | 6 ++++++ fig/service.py | 7 ++++--- tests/service_test.py | 5 +++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/docs/yml.md b/docs/yml.md index df462b17..24484d4f 100644 --- a/docs/yml.md +++ b/docs/yml.md @@ -44,6 +44,12 @@ ports: - "8000:8000" - "49100:22" +-- Expose ports without publishing them to the host machine - they'll only be +-- accessible to linked services. Only the internal port can be specified. +expose: + - "3000" + - "8000" + -- Map volumes from the host machine (HOST:CONTAINER). volumes: - cache/:/tmp/cache diff --git a/fig/service.py b/fig/service.py index d07f80d1..cfc39367 100644 --- a/fig/service.py +++ b/fig/service.py @@ -39,7 +39,7 @@ class Service(object): if 'image' in options and 'build' in options: raise ConfigError('Service %s has both an image and build path specified. A service can either be built to image or use an existing image, not both.' % name) - supported_options = DOCKER_CONFIG_KEYS + ['build'] + supported_options = DOCKER_CONFIG_KEYS + ['build', 'expose'] for k in options: if k not in supported_options: @@ -246,9 +246,10 @@ class Service(object): container_options['name'] = self.next_container_name(one_off) - if 'ports' in container_options: + if 'ports' in container_options or 'expose' in self.options: ports = [] - for port in container_options['ports']: + all_ports = container_options.get('ports', []) + self.options.get('expose', []) + for port in all_ports: port = str(port) if ':' in port: port = port.split(':')[-1] diff --git a/tests/service_test.py b/tests/service_test.py index 458de676..3448596d 100644 --- a/tests/service_test.py +++ b/tests/service_test.py @@ -212,6 +212,11 @@ class ServiceTest(DockerClientTestCase): self.assertEqual(list(container['NetworkSettings']['Ports'].keys()), ['8000/tcp']) self.assertNotEqual(container['NetworkSettings']['Ports']['8000/tcp'][0]['HostPort'], '8000') + def test_expose_does_not_publish_ports(self): + service = self.create_service('web', expose=[8000]) + container = service.start_container().inspect() + self.assertEqual(container['NetworkSettings']['Ports'], {'8000/tcp': None}) + def test_start_container_creates_port_with_explicit_protocol(self): service = self.create_service('web', ports=['8000/udp']) container = service.start_container().inspect() From 5f5fbb3ea4db38c379494598359ee24d674775fe Mon Sep 17 00:00:00 2001 From: Aanand Prasad Date: Tue, 4 Mar 2014 18:00:46 +0000 Subject: [PATCH 3/4] Display unpublished ports in 'fig ps' --- fig/container.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/fig/container.py b/fig/container.py index 0a118af6..c926d2a9 100644 --- a/fig/container.py +++ b/fig/container.py @@ -70,6 +70,8 @@ class Container(object): for private, public in list(self.dictionary['NetworkSettings']['Ports'].items()): if public: ports.append('%s->%s' % (public[0]['HostPort'], private)) + else: + ports.append(private) return ', '.join(ports) @property From d1a52d2b1a5d1e2bd62d9671b636079ff9765d79 Mon Sep 17 00:00:00 2001 From: Aanand Prasad Date: Tue, 4 Mar 2014 21:54:10 +0000 Subject: [PATCH 4/4] Remove unneeded 'ports' entries from WP and Django fig.ymls in docs --- docs/django.md | 2 -- docs/wordpress.md | 2 -- 2 files changed, 4 deletions(-) diff --git a/docs/django.md b/docs/django.md index 8ecb3641..c85eb507 100644 --- a/docs/django.md +++ b/docs/django.md @@ -28,8 +28,6 @@ Simple enough. Finally, this is all tied together with a file called `fig.yml`. db: image: orchardup/postgresql - ports: - - "5432" web: build: . command: python manage.py runserver 0.0.0.0:8000 diff --git a/docs/wordpress.md b/docs/wordpress.md index a2f1bcf4..5eff3487 100644 --- a/docs/wordpress.md +++ b/docs/wordpress.md @@ -33,8 +33,6 @@ web: - .:/code db: image: orchardup/mysql - ports: - - "3306:3306" environment: MYSQL_DATABASE: wordpress ```