瀏覽代碼

added the extra_hosts option to the yml configuration which exposes the --add-host flag from the docker client

Signed-off-by: Sam Wing <[email protected]>
Sam Wing 10 年之前
父節點
當前提交
fb81c37ca6
共有 4 個文件被更改,包括 47 次插入0 次删除
  1. 2 0
      compose/config.py
  2. 14 0
      compose/service.py
  3. 17 0
      docs/yml.md
  4. 14 0
      tests/integration/service_test.py

+ 2 - 0
compose/config.py

@@ -15,6 +15,7 @@ DOCKER_CONFIG_KEYS = [
     'entrypoint',
     'env_file',
     'environment',
+    'extra_hosts',
     'hostname',
     'image',
     'links',
@@ -41,6 +42,7 @@ ALLOWED_KEYS = DOCKER_CONFIG_KEYS + [
 
 DOCKER_CONFIG_HINTS = {
     'cpu_share': 'cpu_shares',
+    'add_host': 'extra_hosts',
     'link': 'links',
     'port': 'ports',
     'privilege': 'privileged',

+ 14 - 0
compose/service.py

@@ -23,6 +23,7 @@ DOCKER_START_KEYS = [
     'dns',
     'dns_search',
     'env_file',
+    'extra_hosts',
     'net',
     'pid',
     'privileged',
@@ -448,6 +449,8 @@ class Service(object):
 
         restart = parse_restart_spec(options.get('restart', None))
 
+        extra_hosts = build_extra_hosts(options.get('extra_hosts', None))
+
         return create_host_config(
             links=self._get_links(link_to_self=one_off),
             port_bindings=port_bindings,
@@ -460,6 +463,7 @@ class Service(object):
             restart_policy=restart,
             cap_add=cap_add,
             cap_drop=cap_drop,
+            extra_hosts=extra_hosts,
             pid_mode=pid
         )
 
@@ -619,3 +623,13 @@ def split_port(port):
 
     external_ip, external_port, internal_port = parts
     return internal_port, (external_ip, external_port or None)
+
+
+def build_extra_hosts(extra_hosts_config):
+    if extra_hosts_config is None:
+        return None
+
+    if isinstance(extra_hosts_config, list):
+        return dict(r.split(':') for r in extra_hosts_config)
+    else:
+        return dict([extra_hosts_config.split(':')])

+ 17 - 0
docs/yml.md

@@ -87,6 +87,23 @@ external_links:
  - project_db_1:postgresql
 ```
 
+### extra_hosts
+
+Add hostname mappings. Use the same values as the docker client `--add-hosts` parameter.
+
+```
+extra_hosts:
+ - docker: 162.242.195.82
+ - fig: 50.31.209.229
+```
+
+An entry with the ip address and hostname will be created in `/etc/hosts` inside containers for this service, e.g:
+
+```
+162.242.195.82  docker
+50.31.209.229   fig
+```
+
 ### ports
 
 Expose ports. Either specify both ports (`HOST:CONTAINER`), or just the container

+ 14 - 0
tests/integration/service_test.py

@@ -107,6 +107,20 @@ class ServiceTest(DockerClientTestCase):
         service.start_container(container)
         self.assertEqual(container.inspect()['Config']['CpuShares'], 73)
 
+    def test_create_container_with_extra_hosts_list(self):
+        extra_hosts = ['docker:162.242.195.82', 'fig:50.31.209.229']
+        service = self.create_service('db', extra_hosts=extra_hosts)
+        container = service.create_container()
+        service.start_container(container)
+        self.assertEqual(container.get('HostConfig.ExtraHosts'), extra_hosts)
+
+    def test_create_container_with_extra_hosts_string(self):
+        extra_hosts = 'docker:162.242.195.82'
+        service = self.create_service('db', extra_hosts=extra_hosts)
+        container = service.create_container()
+        service.start_container(container)
+        self.assertEqual(container.get('HostConfig.ExtraHosts'), [extra_hosts])
+
     def test_create_container_with_specified_volume(self):
         host_path = '/tmp/host-path'
         container_path = '/container-path'