Skip to content
This repository has been archived by the owner on Dec 11, 2020. It is now read-only.

docker_container's ip_address is empty when using network_mode #9

Closed
hashibot opened this issue Jun 13, 2017 · 5 comments
Closed

docker_container's ip_address is empty when using network_mode #9

hashibot opened this issue Jun 13, 2017 · 5 comments
Labels
Milestone

Comments

@hashibot
Copy link

This issue was originally opened by @rkr-kununu as hashicorp/terraform#8972. It was migrated here as part of the provider split. The original body of the issue is below.


Terraform Version

Terraform v0.7.4

Affected Resource(s)

  • docker_container

Terraform Configuration Files

resource "docker_network" "network" {
  name = "demo"
}

resource "docker_image" "image" {
  name = "gliderlabs/alpine:latest"
  keep_locally = true
}

resource "docker_container" "container" {
  name = "demo-container"

  image = "${docker_image.image.name}"

  network_mode = "${docker_network.network.name}"

  command = [ "sleep", "356d" ]
}

output "ip_address" {
  value = "${docker_container.container.ip_address}"
}

Expected Behavior

I expected that 'ip_address' would be set to ip_address of the docker container.

Actual Behavior

The 'ip_address' was not set.

Important Factoids

I did some poking around the code. It appears that the problem is two-fold.

  1. Running docker-inspect on the running container also shows that 'IPAddress' is also empty.
  2. However, docker-inspect also shows that the "Networks" map also contains all the relevant information (ip address, gateways for each network the container is running in). It also appears that the go-dockerclient terraform's using is parsing this information (see NetworkSettings->Networks). My hope is that this "Network" map can be exposed as part of docker_container's output.
@juneeighteen
Copy link

juneeighteen commented Jul 25, 2018

👍 Just ran into this myself. Once I specified Network_mode and gave a custom network name, docker inspect's IP Address was blank, but the actual container IP address is returned properly with the command: docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $containerName (Terraform v0.11.7)

@mavogel
Copy link
Contributor

mavogel commented Jul 25, 2018

which version of the terraform-provider-docker are you using?

@bhuisgen
Copy link
Contributor

bhuisgen commented Oct 8, 2018

network_mode should be set to a networking mode not a network name/ID.

Please see my commits 587988c and fdad338 in PR #92 to fix the provider bug.

@mavogel mavogel added this to the v1.1.0 milestone Oct 8, 2018
@swetli
Copy link

swetli commented Oct 16, 2018

I can confirm that the problem is present with network_mode=host, Terraform v0.11.8 + provider.docker v1.0.3

@mavogel
Copy link
Contributor

mavogel commented Oct 19, 2018

To implement this feature I used the baseline from #50 and even enhanced it with a new output. Now the IP Addresses can be accessed as follows:

provider "docker" {
  version = "1.1.0"
  host    = "unix:///var/run/docker.sock"
}

resource "docker_network" "network" {
  name            = "demo"
  check_duplicate = "true"
}
resource "docker_network" "network_2" {
  name            = "demo-2"
  check_duplicate = "true"
}

resource "docker_image" "image" {
  name         = "gliderlabs/alpine:latest"
  keep_locally = true
}

resource "docker_container" "container" {
  name         = "demo-container"
  image        = "${docker_image.image.name}"
  network_mode = "bridge"
  networks     = ["demo", "demo-2"]
  command      = ["sleep", "356d"]
}

output "network_data" {
  value = "${docker_container.container.network_data}"
}
// foreach will come 
// https://www.hashicorp.com/blog/hashicorp-terraform-0-12-preview-for-and-for-each
output "ip_addresses" {
  value = "${list(
    lookup(docker_container.container.network_data[0], "ip_address"),
    lookup(docker_container.container.network_data[1], "ip_address")
  )}"
}

which will output you the following format

ip_addresses = [
    172.19.0.2,
    172.18.0.2
]
network_data = [
    {
        gateway = 172.19.0.1,
        ip_address = 172.19.0.2,
        ip_prefix_length = 16,
        network_name = demo-2
    },
    {
        gateway = 172.18.0.1,
        ip_address = 172.18.0.2,
        ip_prefix_length = 16,
        network_name = demo
    }
]

# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
Projects
None yet
Development

No branches or pull requests

5 participants