Open zahornyak opened 8 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}")]))
}
}
@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
Community Note
Terraform (and docker Provider) Version
Terraform v1.5.1 on darwin_amd64
Affected Resource(s)
docker_image
Terraform Configuration Files
Debug Output
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
terraform apply