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

Signed-off-by: Sam Wing <sampwing@gmail.com>
This commit is contained in:
Sam Wing 2015-01-15 12:58:17 -08:00 committed by CJ
parent e6ec76161d
commit fb81c37ca6
4 changed files with 47 additions and 0 deletions

View File

@ -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',

View File

@ -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(':')])

View File

@ -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

View File

@ -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'