diff --git a/compose/config.py b/compose/config.py index f87da1d8c..87d610c82 100644 --- a/compose/config.py +++ b/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', diff --git a/compose/service.py b/compose/service.py index 5afaa30fa..dfc7a71f0 100644 --- a/compose/service.py +++ b/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(':')]) diff --git a/docs/yml.md b/docs/yml.md index c375648df..8756b2020 100644 --- a/docs/yml.md +++ b/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 diff --git a/tests/integration/service_test.py b/tests/integration/service_test.py index 4abd4a909..5bc877d03 100644 --- a/tests/integration/service_test.py +++ b/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'