hashicorp / terraform-provider-docker

As part of our introduction to self-service publishing in the Terraform Registry, this copy of the provider has been archived, and ownership has been transferred to active maintainers in the community. Please see the new location on the Terraform Registry: https://registry.terraform.io/providers/kreuzwerker/docker/latest
https://registry.terraform.io/providers/kreuzwerker/docker/latest
Mozilla Public License 2.0
132 stars 92 forks source link

Windows: docker_container host_path must be an absolute path #277

Open Morodar opened 4 years ago

Morodar commented 4 years ago

Terraform Version

v0.12.28 on Windows 10 Pro Build 18363

Affected Resource(s)

Terraform Configuration Files

provider "docker" {
  host    = "tcp://127.0.0.1:2375/"
  version = "2.7.1"
}

resource "docker_image" "nginx_alpine" {
  name = "nginx:1.19.0-alpine"
}

resource "docker_container" "reverse_proxy" {
  image = docker_image.nginx_alpine.latest
  name  = "reverse_proxy"
  ports {
    internal = 80
    external = var.port
  }
  volumes {
    container_path = "/etc/nginx/conf.d"
    host_path      = abspath(path.root)
    read_only      = true
  }
}

Expected Behavior

What should have happened?

  # module.docker_reverse_proxy_setup.docker_container.reverse_proxy will be created
  + resource "docker_container" "reverse_proxy" {

     [...](omitted)

      + volumes {
          + container_path = "/etc/nginx/conf.d"
          + host_path      = "C:/absolute/root/path/"
          + read_only      = true
        }
    }

Actual Behavior

Error: "volumes.0.host_path" must be an absolute path

  on modules\docker-nginx\resources.tf line 7, in resource "docker_container" "reverse_proxy":
   7: resource "docker_container" "reverse_proxy" {

Steps to Reproduce

  1. terraform apply

Important Factoids

On windows:

host_path = "G:\\ABC" # --> absolute path
host_path = "/G/ABC" # --> absolute path
host_path = abspath(path.root) # --> not an absolute path
host_path = path.root # --> not an absolute path
host_path = G:/ABC # --> not an absolute path

abspath(path.root) returns C:/path/to/root which is an absolute path on Windows and Terraform but not for the docker_container resource.

Am I missing something or is this a bug?
I try to write terraform code which should run both on Windows and Linux.
But I fail to handle absolute paths on Windows correctly.

References

Morodar commented 4 years ago

Workaround (Windows only)

For now, I stick with this hack but it will not work on Linux because of the leading slash.

I replace the : in C:/ with ` nothing. I've also added a leading/`.

So C:/absolute/root/path results in /C/absolute/root/path.

locals {
  // TODO: Windows only Workaround - not recommended!
  root_path = "/${replace(abspath(path.root), ":", "")}"
}
resource "docker_container" "reverse_proxy" {
  image = docker_image.nginx_alpine.latest
  name  = "reverse_proxy"
  volumes {
    container_path = "/etc/nginx/conf.d"
    host_path      = "${local.root_path}/generated/nginx"

    read_only = true
  }
[...]
}

Edit

To take this even further (and to achieve Linux compatibility) I changed it to

locals {
  root_path_tmp = "/${replace(abspath(path.root), ":", "")}"
  root_path     = "${replace(local.root_path_tmp, "////", "/")}"
}

We need to use //// because the first and the last slash indicate the start and the end of a regex.