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

docs: add notes regarding remote builds and context #536

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Tyonar
Copy link

@Tyonar Tyonar commented Mar 26, 2023

Problem to solve

I mistakenly thought I had to copy my Dockerfile and other files relevant to the build to the remote host where I wanted to build the image before the docker_image resource creation.

How to reproduce

$ terraform version
Terraform v1.3.9
on linux_amd64

Summary of Terraform configuration:

terraform {
  required_version = ">= 1.3"
  required_providers {
    docker = {
      source = "kreuzwerker/docker"
      version = "3.0.2"
    }
  }
}

variable "target_host" {
  description = "IP of the VM to target"
  type        = string
  validation {
    condition     = cidrhost("${var.target_host}/32", 0) == var.target_host
    error_message = "target_host must be a valid IP address"
  }
}
provider "docker" {
  host = "ssh://root@${var.target_host}"
}

resource "docker_image" "recursor" {
  name = "recursor"
  build {
    context    = "/opt/recursor"
    tag        = ["recursor:current"]
    dockerfile = "/opt/recursor/Dockerfile"
  }
}

Original error I encountered:

docker_image.recursor: Creating...
docker_image.recursor: Still creating... [10s elapsed]
╷
│ Error: failed to solve with frontend dockerfile.v0: failed to read dockerfile: open /var/lib/docker/tmp/buildkit-mount4152034525/opt/recursor/Dockerfile: no such file or directory
│ 
│ 
│ 
│   with docker_image.recursor,
│   on dns_recursor.tf line 38, in resource "docker_image" "recursor":
│   38: resource "docker_image" "recursor" {
│ 
╵

How to solve

I eventually figured out that the build would work when I set build.context = "." on resource.docker_image.recursor.

I added a few comments in the documentation to clarify that the build context is local even when the provider is configured with a remote host.

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

Successfully merging this pull request may close these issues.

None yet

1 participant