terraform-provider-docker icon indicating copy to clipboard operation
terraform-provider-docker copied to clipboard

Allow range of ports for containers and services

Open mavogel opened this issue 3 years ago • 5 comments

This issue was originally opened by @tomalok as https://github.com/hashicorp/terraform-provider-docker/issues/223. It was migrated here as a result of the community provider takeover from @kreuzwerker. The original body of the issue is below.


Terraform Version

Terraform v0.12.16

Affected Resource(s)

  • docker_container
  • docker_service

Terraform Configuration Files

resource docker_container "exhibit_a" {
  name = "exhibit_a"
  image = "alpine:latest"
  ports {
    protocol = "udp"
    internal = "61000-62000"
    external = "61000-62000"
  }
} 

resource docker_service "exhibit_b" {
  name = "exhibit_b"
  task_spec {
    container_spec {
      image = "alpine:latest"
    }
  }
  endpoint_spec {
    ports {
      name = "MOSH"
      protocol = "udp"
      target_port = "61000-62000"
      published_port = "61000-62000"
    }
  }
}

Expected Behavior

The Docker provider should accept the string specifying a range of ports, which Docker itself has supported since v1.5 or thereabouts.

Actual Behavior

Error: Incorrect attribute value type

  on main.tf line 176, in resource "docker_service" "this":
 176:         target_port     = p.value.target

Inappropriate value for attribute "target_port": a number is required.

Error: Incorrect attribute value type

  on main.tf line 177, in resource "docker_service" "this":
 177:         published_port  = p.value.publish

Inappropriate value for attribute "published_port": a number is required.

mavogel avatar Dec 25 '20 19:12 mavogel

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days. If you don't want this issue to be closed, please set the label pinned.

github-actions[bot] avatar Mar 29 '21 10:03 github-actions[bot]

To my knowledge this hasn't yet been addressed.

tomalok avatar Mar 29 '21 15:03 tomalok

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days. If you don't want this issue to be closed, please set the label pinned.

github-actions[bot] avatar May 29 '21 10:05 github-actions[bot]

checked vs latest terraform/docker provider

jake@jimini port-range % terraform version
Terraform v0.15.4
on darwin_amd64
+ provider registry.terraform.io/kreuzwerker/docker v2.12.2
jake@jimini port-range % terraform plan
╷
│ Error: Incorrect attribute value type
│ 
│   on port-range.tf line 18, in resource "docker_container" "exhibit_a":
│   18:     internal = "61000-62000"
│ 
│ Inappropriate value for attribute "internal": a number is required.
╵
╷
│ Error: Incorrect attribute value type
│ 
│   on port-range.tf line 19, in resource "docker_container" "exhibit_a":
│   19:     external = "61000-62000"
│ 
│ Inappropriate value for attribute "external": a number is required.
╵
╷
│ Error: Incorrect attribute value type
│ 
│   on port-range.tf line 34, in resource "docker_service" "exhibit_b":
│   34:       target_port = "61000-62000"
│ 
│ Inappropriate value for attribute "target_port": a number is required.
╵
╷
│ Error: Incorrect attribute value type
│ 
│   on port-range.tf line 35, in resource "docker_service" "exhibit_b":
│   35:       published_port = "61000-62000"
│ 
│ Inappropriate value for attribute "published_port": a number is required.
╵

tomalok avatar May 29 '21 21:05 tomalok

Will be done together with #88 where we might also consider using dynamic blocks for this instead of a string parsing

mavogel avatar Jun 23 '21 09:06 mavogel

How's the progress for this issue? It's been 1.5 years

mmgfrcs avatar Nov 09 '22 08:11 mmgfrcs

This is actually not possible to implement. Having a string with a range of ports, parsing that and send it to the docker daemon would be a parsing mess, but nothing complicated. However, we also have to map the information of the docker inspect $containerId back to the terraform state. And that's where it gets interesting. The information returned is:

"Ports": {
                "1234/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "1234"
                    }
                ],
                "1235/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "1235"
                    }
                ],
                "1236/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "1236"
                    }
                ]
            },

Now, the question is: Was that created from a single port range ("1234-1236:1234-1236") or from three different ports blocks? We cannot find that out.

And having port ranges is actually possible with the current approach! You could use dynamic blocks that should be a nice workaround.

Closing this

Junkern avatar Jan 05 '23 10:01 Junkern