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

docker_registry_image build.context fails because it changes the absolute path, which doesn't exist

Open KanchiMoe opened this issue 3 years ago • 13 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 version
Terraform v1.0.4
on windows_amd64
+ provider registry.terraform.io/hashicorp/aws v3.49.0
+ provider registry.terraform.io/hashicorp/random v3.1.0
+ provider registry.terraform.io/hashicorp/template v2.2.0
+ provider registry.terraform.io/kreuzwerker/docker v2.15.0
$ docker version
Client:
 Cloud integration: 1.0.17
 Version:           20.10.8
 API version:       1.41
 Go version:        go1.16.6
 Git commit:        3967b7d
 Built:             Fri Jul 30 19:58:50 2021
 OS/Arch:           windows/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.8
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.16.6
  Git commit:       75249d8
  Built:            Fri Jul 30 19:52:10 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.9
  GitCommit:        e25210fe30a0a703442421b0f60afac609f950a3
 runc:
  Version:          1.0.1
  GitCommit:        v1.0.1-0-g4144b63
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

Affected Resource(s)

  • docker_registry_image

Terraform Configuration Files

resource "docker_registry_image" "kcm-docker-image" {
  name = "kcm-gitlab-d"

  build {
    context = "/src"
    dockerfile = "dockerfile"

    auth_config {

The only important line here for this bug is context = "/src". Addational code may be needed to get it to work, a copy of such can be found on my repo here.

Debug Output

      + sha256_digest        = (known after apply)

      + build {
          + context    = "/src:"
          + dockerfile = "dockerfile"

          + auth_config {
              + auth           = (sensitive)

The only line that is important is + context = "/src:".

Panic Output

N/a

Expected Behaviour

The + context like should have the value /src.

Actual Behaviour

An error is generated.

Error: Error building docker image: unable to build context: unable to read build context - CreateFile /src: The system cannot find the file specified.

   with docker_registry_image.kcm-docker-image,
   on ecr.tf line 19, in resource "docker_registry_image" "kcm-docker-image":
   19: resource "docker_registry_image" "kcm-docker-image" {

In the output, an addational : is added, and sometimes, a UID. This value is described in the documentation as being

The absolute path to the context folder

This means, because of the added colon, the path goes from

C:\Users\foo\bar\baz to C:\Users\foo\bar\baz:

Which doesn't exist, causing terraform to fail.

It looks like this is produced by line 252 of internal/provider/resource_docker_registry_image.go, where for some reason, it adds information to the absolute, which doesn't exist, causing it to fail.

Steps to Reproduce

  1. Use the code/repo I've linked above.
  2. Run terraform apply.

Important Factoids

I am running this on a Windows 10 machine. I'm not sure how Linux or other systems handle this issue.

KanchiMoe avatar Sep 26 '21 06:09 KanchiMoe

I am also having the same issue. I am using windows 10. It adds some random stirng ":ce23c844d89a80df67d605f14cf97962e3e8541858022a7772a2a0c38b07ae8e" after my folder path.

Terraform v1.0.5 Docker version 20.10.2, build 2291f61

pThonprogrammer avatar Nov 30 '21 20:11 pThonprogrammer

Having the same issue on Windows 10 and macOS Monterey. The only way I can get rid of it is to terraform apply -auto-approve as I think having a local planfile is screwing up the hash since my terraform folder is inside the folder for the Dockerfile.

I tried a variety of ways to strip the hash out of the context using regex and string functions with no avail.

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days. If you don't want this issue to be closed, please set the label pinned.

github-actions[bot] avatar Feb 14 '22 10:02 github-actions[bot]

This seems to be caused by CreateFile /src:. The : in a file name means a alternate data stream in windows which is why this is failing: https://blog.malwarebytes.com/101/2015/07/introduction-to-alternate-data-streams/

The solution is to use any character besides : in the filename when building this string.

zwinnerman-fleetdm avatar Feb 14 '22 20:02 zwinnerman-fleetdm

Commenting to remove the stale tag.

KanchiMoe avatar Feb 14 '22 22:02 KanchiMoe

Experiencing the same on an Arch Linux machine. This is not unique to Windows machines it seems.

A colon, sometimes also a UID, is appended to the absolute path, causing the error.

vegarab avatar Feb 22 '22 10:02 vegarab

Experiencing the same on Amazon Linux 2. I've tried avoiding a terraform plan and going straight to auto-approval; didn't resolve my issue. I've tried using a Dockerfile outside the directory of where my terraform files are; didn't work either.

A colon and UID, in my case is, always appended, and they differ in terraforms plan and apply.

rdeavilafloqast avatar Feb 22 '22 19:02 rdeavilafloqast

do you have any solution to this problem? I have removed the plan file from the repository and disable saving the cache but still seeing this problem.

gaborbalogh1 avatar Mar 24 '22 15:03 gaborbalogh1

do you have any solution to this problem? I have removed the plan file from the repository and disable saving the cache but still seeing this problem.

So since no one has an idea on how to fix it here is a workaround I have found working well in my projects after experimenting with the build context path:

original path was:

context = "${path.cwd}" , "${path.cwd}/" or "${abspath(path.cwd)}/"

new path :

context = "${path.cwd}/FolderName/"

Thanks,

gaborbalogh1 avatar Apr 07 '22 09:04 gaborbalogh1

@rdeavilafloqast with v2.18.1it is now possible to have Dockerfiles outside the context.

@KanchiMoe in your exact use case it is failing because the absolute path /src does not exist. In the copy of the repo there is an src repo on the same level. I think it would work if you specify ${path.cwd}/src (as written in the documentation https://registry.terraform.io/providers/kreuzwerker/docker/latest/docs/resources/registry_image#context)

I general, however, we want to refactor this whole thing as soon as possible. From what I know, the UUID is the hash of the while context path. This was added in order to know, when the contents of the context change. Adding this to original value was not the best idea, as we can see by all the issues with "inconsistent plan".

The idea I am having is to introduce a new context_hash computed property, which will contain the directory hash. That way the original context value will not be changed. However, this will probably be a breaking change and needs thorough planning. We might even overhaul the whole docker_registry_image & docker_image architecture

Junkern avatar Jul 14 '22 14:07 Junkern

@Junkern how about just adding either a source_code_hash like the lamda resource has, or triggers like in the null_resource provider has? That way we can always override the behavior ourselves if it's ever not working as we'd expect for some reason.

glenngillen avatar Aug 06 '22 11:08 glenngillen

@glenngillen that could actually be a viable, not very complicated to implement, solution. 🤔 The more I think about that, the more I like it.

Junkern avatar Aug 08 '22 15:08 Junkern

If you dont want to change your build context to another directory to workaround this problem, you can create a .dockerignore file which would ignore anything other than what is necessary to build. This way the context hash stays the same during terraform apply step.

barankaraaslan avatar Sep 08 '22 09:09 barankaraaslan

The build block of docker_registry_image is now deprecated and will be removed with the next major version. Please migrate to using the build block of the docker_image. There were many fixes done to the context attribute which should fix most of the problems. There will also be a guide for migrating from v2.x to v3.x.

Please open a new issue in case you encounter any bugs and issues!

Junkern avatar Jan 05 '23 14:01 Junkern