kreuzwerker/terraform-provider-docker

Dockerfile path does not work

zahornyak opened this issue ยท 7 comments

Community Note

  • Please vote on this issue by adding a ๐Ÿ‘ reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or "me too" comments, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Terraform (and docker Provider) Version

Terraform v1.5.1
on darwin_amd64

  • provider registry.terraform.io/hashicorp/aws v4.67.0
  • provider registry.terraform.io/hashicorp/external v2.3.1
  • provider registry.terraform.io/hashicorp/local v2.4.0
  • provider registry.terraform.io/hashicorp/null v3.2.1
  • provider registry.terraform.io/hashicorp/template v2.1.2
  • provider registry.terraform.io/kreuzwerker/docker v3.0.2
  • provider registry.terraform.io/philips-labs/kong v6.630.0

Affected Resource(s)

  • docker_image

Terraform Configuration Files

resource "docker_image" "dockerfile" {
  name = "${aws_ecr_repository.ecr.repository_url}:latest"

  build {
    context = "${path.module}/files/"
    tag     = ["${aws_ecr_repository.ecr.repository_url}:latest"]
    dockerfile = "${path.module}/files/Dockerfile"
  }

  triggers = {
    prom_sha = base64sha256(file("${path.module}/files/Dockerfile"))
  }
}

Debug Output

โ•ท
โ”‚ Error: failed to solve with frontend dockerfile.v0: failed to read dockerfile: open /var/lib/docker/tmp/buildkit-mount3430792059/templates/service/files/Dockerfile: no such file or directory
โ”‚ 
โ”‚ 
โ”‚ 
โ”‚   with module.service.docker_image.dockerfile[0],
โ”‚   on ../../../templates/service/docker.tf line 1, in resource "docker_image" "dockerfile":
โ”‚    1: resource "docker_image" "dockerfile" {
โ”‚ 
โ•ต

Expected Behaviour

Building an image from Dockerfile provided by path in module

Actual Behaviour

Error failed to read dockerfile : no such file or directory

Steps to Reproduce

set the path to your dockerfile(not just "Dockerfile") in module

dockerfile = "path/to/Dockerfile"
  1. terraform apply

@zahornyak try this:

resource "docker_image" "dockerfile" {
  name = "${aws_ecr_repository.ecr.repository_url}:latest"

  build {
    context = "${path.module}/files"
    tag     = ["${aws_ecr_repository.ecr.repository_url}:latest"]
  }

  triggers = {
    dir_sha1 = sha1(join("", [for f in fileset("${path.module}/files", "**") : filesha1("${path.module}/files/${f}")]))
  }
}

@shoootyou Hi, thanks for your response but the main point is to use dockerfile= variable to set the path to dockerfile. At the moment it works with dockerfile, which is located in the working directory but if I use it inside the module, it wont work. I've checked all the paths I set and it is correct, but the provider says it is not.
BTW it looks like under the hood it creates /var/lib/docker/tmp/buildkit-mount3430792059 with only the current working directory inside and the dockerfile is not there obviously because it is in the module folder.

@zahornyak I think if you use the Dockerfile inside of the module you need to specify just "files" in context, something like this:

  build {
    context = "files/"
    tag     = ["${aws_ecr_repository.ecr.repository_url}:latest"]
    dockerfile = "files/Dockerfile"
  }

This originates because the value of path.module depends on the execution. If you run it in the root of your code and it calls the module that has this config, the value of path.module will be the root of your code, not the root of your module. Check this: https://developer.hashicorp.com/terraform/language/expressions/references

@shoootyou Thanks for the response. I've got it works by setting context = path.module and dockerfile = "files/Dockerfile" and it works. Looks like it takes all the files from context path, so you cannot use it like you are using docker commands docker build -t sometag -f ../../files/Dockerfile . So thanks for that, you were right about the context files

There is a test of this behaviour:

build {
context = "."
dockerfile = "../Dockerfile"
}
}

seems it should work?

It is intended to: #401, #402

But behaviour's inconsistent:

  • dockerfile is inside context: path must be relative to context, e.g. "Dockerfile"
  • dockerfile is outside context: path must be absolute, e.g. "${path.root}/../elsewhere/Dockerfile"

i.e. in the latter case, if context is "${path.root}/somewhere", it cannot be "../../elsewhere/Dockerfile".

Which makes sense in isolation / is a usual way to terraform - what's weird is the first case, where it also can't be "${path.root}/somewhere/Dockerfile" - because it's inside context it now has to be relative to that, and drop the prefix.

(It's especially confusing if you have a module for dealing with your images, these are inputs to it, and you have some of each case...)

I've come across this problem, I've created an extra issue as I did not come across this straightaway. #650