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

Windows: docker_container host_path must be an absolute path

Open mavogel opened this issue 3 years ago • 8 comments

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


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

  • https://github.com/hashicorp/terraform/issues/14986#issuecomment-448756885

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]

Environment

  • MacOS Big Sur
  • Terraform v0.14
  • Docker for mac

File Structure

  • main.tf
  • variables.tf ( contains host path absolute path )

Relevant code snapshot

main.tf

resource "docker_container" "nginx"{
image = docker_image.nginx.name
name = "nginx-local-server"
volumes {
host_path = docker_volume.shared_volume.name
container_path = var.nginx_container_volume_mount
}
ports {
internal = var.nginx_internal_port
external = var.nginx_external_port
ip = var.nginx_ip
}
}

resource "docker_volume" "shared_volume"
{
name = var.nginx_host_volume
}

variables.tf

variable "nginx_host_volume"
{
description = "Nginx docker host volume"
type = string
default = "/Users/omri/dev/Local_Infrastracture/configs/nginx_conf.d"
}

Output error after 'terraform init' -> 'terraform apply'

Error: Unable to create volume: Error response from daemon: create /Users/omri/dev/terraform-dev/Local_Infrastracture/configs/nginx_conf.d: "/Users/omri/dev/terraform-dev/Local_Infrastracture/configs/nginx_conf.d" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed. If you intended to pass a host directory, use absolute path

This is still an ongoing issue, can someone please update on the matter and how should we progress?

EpicApex avatar Apr 19 '21 12:04 EpicApex

We do not have a windows machine atm to test this but will consider windows tests to reproduce the issue.

mavogel avatar Apr 19 '21 16:04 mavogel

We do not have a windows machine atm to test this but will consider windows tests to reproduce the issue.

What about the macOS distribution - do you have any inputs on this? (The issue persists)

I can generate what ever example is needed - but it has the same behavior in "macOS" and "Windows"

EpicApex avatar Apr 19 '21 16:04 EpicApex

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 Jun 20 '21 10:06 github-actions[bot]

related to #125

mavogel avatar Jun 20 '21 12:06 mavogel

I'm facing the same problem reported here by @mavogel on Windows.

I've just discovered that the RegEx here will never work when a path is similar to the following:

Example Path:

G:/ABC

Regular Expression in the validator:

^[a-zA-Z]:\\|^/)`

The case G:/ is never handled

madduci avatar Aug 19 '22 03:08 madduci

Made a PR (#438) for it

madduci avatar Aug 19 '22 04:08 madduci

(Hopefully) fixed by #438 Will be released in a new version later this week

Junkern avatar Aug 29 '22 10:08 Junkern