浏览代码

Support 'expose' config option, like docker's --expose

Exposes ports to linked services without publishing them to the world.
Aanand Prasad 11 年之前
父节点
当前提交
2d98071e55
共有 3 个文件被更改,包括 15 次插入3 次删除
  1. 6 0
      docs/yml.md
  2. 4 3
      fig/service.py
  3. 5 0
      tests/service_test.py

+ 6 - 0
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

+ 4 - 3
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]

+ 5 - 0
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()