kreuzwerker / terraform-provider-docker

Terraform Docker provider
Mozilla Public License 2.0
570 stars 187 forks source link

provider ignoring context if a dockerfile exists in the current directory #592

Open domoran opened 6 months ago

domoran commented 6 months ago

Community Note

Terraform (and docker Provider) Version

Terraform v1.3.7 on windows_amd64

Affected Resource(s)

Terraform Configuration Files

terraform {
  required_providers {
    docker = {
      source  = "kreuzwerker/docker"
      version = "3.0.2"
    }
  }
}

provider "docker" {
}

# Comment that out, and the docker provider will resolve the correct Dockerfile!
resource "local_file" "bad_dockerfile" {
  content  = ""
  filename = "${path.module}/Dockerfile"
}

resource "local_file" "dockerfile" {
  content  = "from ubuntu:latest"
  filename = "${path.module}/.context/Dockerfile"
}

resource "docker_image" "image" {
  name = "bug"
  build {
    context = dirname(local_file.dockerfile.filename)
    tag     = ["webserver:latest"]
  }
}

Debug Output

https://gist.github.com/domoran/1188b6754fd497be697cbf54b1a416bb

The relevant part of the log is:

2024-01-03T11:06:49.703+0100 [INFO] provider.terraform-provider-docker_v3.0.2.exe: 2024/01/03 11:06:49 [DEBUG] relDockerfile ..\Dockerfile: timestamp=2024-01-03T11:06:49.702+0100

where the provider is choosing an invalid Dockerfile location, based on the existence of another Dockerfile in the current directory.

Expected Behaviour

The docker provider should look for a Dockerfile inside the context and ignore the dockerfile inside the current directory. The search location should be consistent whether or not that other Dockerfile exists.

Actual Behaviour

When the empty Dockerfile in the current directory exists the docker provider picks up that file (and fails) even though context is set to .context directory.

Steps to Reproduce

  1. terraform apply

The docker build fails with the error message "failed to create LLB definition: the Dockerfile cannot be empty"

  1. terraform destroy

Now comment out the bad dockerfile resource

# Comment that out, and it will work
#resource "local_file" "bad_dockerfile" {
#  content  = ""
#  filename = "${path.module}/Dockerfile"
#}
  1. terraform apply Now the build works.