Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dockerfile path does not work #585

Open
zahornyak opened this issue Oct 30, 2023 · 4 comments
Open

Dockerfile path does not work #585

zahornyak opened this issue Oct 30, 2023 · 4 comments

Comments

@zahornyak
Copy link

zahornyak commented Oct 30, 2023

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
@shoootyou
Copy link

@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
Copy link
Author

@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
Copy link

@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
Copy link
Author

zahornyak commented Nov 22, 2023

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants