diff --git a/builtin/providers/docker/resource_docker_container.go b/builtin/providers/docker/resource_docker_container.go index 242462e1a..6ad5176ac 100644 --- a/builtin/providers/docker/resource_docker_container.go +++ b/builtin/providers/docker/resource_docker_container.go @@ -130,6 +130,28 @@ func resourceDockerContainer() *schema.Resource { Set: resourceDockerPortsHash, }, + "hosts": &schema.Schema{ + Type: schema.TypeSet, + Optional: true, + ForceNew: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "ip": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + + "host": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + }, + }, + Set: resourceDockerHostsHash, + }, + "env": &schema.Schema{ Type: schema.TypeSet, Optional: true, @@ -323,6 +345,21 @@ func resourceDockerPortsHash(v interface{}) int { return hashcode.String(buf.String()) } +func resourceDockerHostsHash(v interface{}) int { + var buf bytes.Buffer + m := v.(map[string]interface{}) + + if v, ok := m["ip"]; ok { + buf.WriteString(fmt.Sprintf("%v-", v.(string))) + } + + if v, ok := m["host"]; ok { + buf.WriteString(fmt.Sprintf("%v-", v.(string))) + } + + return hashcode.String(buf.String()) +} + func resourceDockerVolumesHash(v interface{}) int { var buf bytes.Buffer m := v.(map[string]interface{}) diff --git a/builtin/providers/docker/resource_docker_container_funcs.go b/builtin/providers/docker/resource_docker_container_funcs.go index 814941bba..bf2bc9e2f 100644 --- a/builtin/providers/docker/resource_docker_container_funcs.go +++ b/builtin/providers/docker/resource_docker_container_funcs.go @@ -67,6 +67,11 @@ func resourceDockerContainerCreate(d *schema.ResourceData, meta interface{}) err createOpts.Config.ExposedPorts = exposedPorts } + extraHosts := []string{} + if v, ok := d.GetOk("extra_hosts"); ok { + extraHosts = extraHostsSetToDockerExtraHosts(v.(*schema.Set)) + } + volumes := map[string]struct{}{} binds := []string{} volumesFrom := []string{} @@ -100,7 +105,9 @@ func resourceDockerContainerCreate(d *schema.ResourceData, meta interface{}) err if len(portBindings) != 0 { hostConfig.PortBindings = portBindings } - + if len(extraHosts) != 0 { + hostConfig.ExtraHosts = extraHosts + } if len(binds) != 0 { hostConfig.Binds = binds } @@ -312,6 +319,19 @@ func portSetToDockerPorts(ports *schema.Set) (map[dc.Port]struct{}, map[dc.Port] return retExposedPorts, retPortBindings } +func extraHostsSetToDockerExtraHosts(extraHosts *schema.Set) []string { + retExtraHosts := []string{} + + for _, hostInt := range extraHosts.List() { + host := hostInt.(map[string]interface{}) + ip := host["ip"].(string) + hostname := host["host"].(string) + retExtraHosts = append(retExtraHosts, hostname+":"+ip) + } + + return retExtraHosts +} + func volumeSetToDockerVolumes(volumes *schema.Set) (map[string]struct{}, []string, []string, error) { retVolumeMap := map[string]struct{}{} retHostConfigBinds := []string{} diff --git a/website/source/docs/providers/docker/r/container.html.markdown b/website/source/docs/providers/docker/r/container.html.markdown index 920288eb2..2f23d3db3 100644 --- a/website/source/docs/providers/docker/r/container.html.markdown +++ b/website/source/docs/providers/docker/r/container.html.markdown @@ -57,6 +57,7 @@ The following arguments are supported: kept running. If false, then as long as the container exists, Terraform assumes it is successful. * `ports` - (Optional) See [Ports](#ports) below for details. +* `extra_hosts` - (Optional) See [Extra Hosts](#extra_hosts) below for details. * `privileged` - (Optional, bool) Run container in privileged mode. * `publish_all_ports` - (Optional, bool) Publish all ports of the container. * `volumes` - (Optional) See [Volumes](#volumes) below for details. @@ -82,6 +83,16 @@ the following: * `protocol` - (Optional, string) Protocol that can be used over this port, defaults to TCP. + +## Extra Hosts + +`extra_hosts` is a block within the configuration that can be repeated to specify +the extra host mappings for the container. Each `extra_hosts` block supports +the following: + +* `host` - (Required, int) Hostname to add. +* `ip` - (Required, int) IP address this hostname should resolve to.. + ## Volumes