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

Dockerfile path does not work

Open zahornyak opened this issue 1 year ago • 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 avatar Oct 30 '23 15:10 zahornyak

@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 avatar Nov 17 '23 03:11 shoootyou

@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 avatar Nov 17 '23 15:11 zahornyak

@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 avatar Nov 17 '23 18:11 shoootyou

@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

zahornyak avatar Nov 22 '23 16:11 zahornyak