kreuzwerker / terraform-provider-docker

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

Dockerfile path does not work #585

Open zahornyak opened 8 months ago

zahornyak commented 8 months ago

Community Note

Terraform (and docker Provider) Version

Terraform v1.5.1 on darwin_amd64

Affected Resource(s)

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
shoootyou commented 7 months ago

@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}")]))
  }
}
zahornyak commented 7 months ago

@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.

shoootyou commented 7 months ago

@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

zahornyak commented 7 months ago

@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