-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Add extra_hosts to yml configuration --add-hosts #1158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
fb81c37
8098b65
25ee3f0
86a08c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ | |
| 'dns', | ||
| 'dns_search', | ||
| 'env_file', | ||
| 'extra_hosts', | ||
| 'net', | ||
| 'pid', | ||
| 'privileged', | ||
|
|
@@ -448,6 +449,8 @@ def _get_container_host_config(self, override_options, one_off=False, intermedia | |
|
|
||
| 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 @@ def _get_container_host_config(self, override_options, one_off=False, intermedia | |
| restart_policy=restart, | ||
| cap_add=cap_add, | ||
| cap_drop=cap_drop, | ||
| extra_hosts=extra_hosts, | ||
| pid_mode=pid | ||
| ) | ||
|
|
||
|
|
@@ -619,3 +623,28 @@ 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 not extra_hosts_config: | ||
| return {} | ||
|
|
||
| if isinstance(extra_hosts_config, list): | ||
| extra_hosts_dict = {} | ||
| for extra_hosts_line in extra_hosts_config: | ||
| if not isinstance(extra_hosts_line, six.string_types): | ||
| raise ConfigError( | ||
| "extra_hosts_config \"%s\" must be either a list of strings or a string->string mapping," % | ||
| extra_hosts_config | ||
| ) | ||
| host, ip = extra_hosts_line.split(':') | ||
| extra_hosts_dict.update({host.strip(): ip.strip()}) | ||
| extra_hosts_config = extra_hosts_dict | ||
|
|
||
| if isinstance(extra_hosts_config, dict): | ||
| return extra_hosts_config | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be a good idea to only support one of these formats. We've supported both in the past, but I'm not sure that's necessary going forward. It seems to cause confusion and extra code. If we decide to keep lists (or both), I would suggest If we decide to keep mappings (or both), we should probably do some basic validation of the keys and values, maybe even just assert that they are Truthy (not None or empty strings). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I just realized this discussion has already happened. I'll defer to whatever @aanand decides now that I've registered my concerns. |
||
|
|
||
| raise ConfigError( | ||
| "extra_hosts_config \"%s\" must be either a list of strings or a string->string mapping," % | ||
| extra_hosts_config | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -87,6 +87,23 @@ external_links: | |
| - project_db_1:postgresql | ||
| ``` | ||
|
|
||
| ### extra_hosts | ||
|
|
||
| Add hostname mappings. Use the same values as the docker client `--add-host` parameter. | ||
|
|
||
| ``` | ||
| extra_hosts: | ||
| - "somehost:162.242.195.82" | ||
| - "otherhost:50.31.209.229" | ||
| ``` | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "docker" and "fig" are strange host names - why would I have a host called "fig"? (cc @moxiegirl - any best practices here?) Could you add an example with a dict? Best if the strings are quoted, e.g.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated examples |
||
|
|
||
| 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 somehost | ||
| 50.31.209.229 otherhost | ||
| ``` | ||
|
|
||
| ### ports | ||
|
|
||
| Expose ports. Either specify both ports (`HOST:CONTAINER`), or just the container | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe
'extra_host': 'extra_hosts',as well ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added @ 86a08c0