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

Windows: docker_container host_path must be an absolute path #277

Open
Morodar opened this issue Jul 4, 2020 · 1 comment
Open

Windows: docker_container host_path must be an absolute path #277

Morodar opened this issue Jul 4, 2020 · 1 comment

Comments

@Morodar
Copy link

Morodar commented Jul 4, 2020

Terraform Version

v0.12.28 on Windows 10 Pro Build 18363

Affected Resource(s)

  • docker_container

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
Copy link
Author

Morodar commented Jul 5, 2020

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.

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

No branches or pull requests

1 participant