dockerterraformdocker-desktopopentofu

Tofu/Terraform Docker provider error: failed to read downloaded context: failed to load cache key: Get "http://build-context-xxx": context not found


I'm currently trying to build a docker image with openTofu but the build always fail with context not found. The exact error is: Error: failed to read downloaded context: failed to load cache key: Get "http://build-context-ukmuzy8fmy9yw8e75cbqrc43c": context not found

Tofu resource:

resource "docker_image" "test" {
    name  = "test_context"
    build {
        context      = "${local.assetsPath}/testContext/"
        pull_parent  = true
        remove       = false
        force_remove = false
        no_cache     = true
        platform     = "linux/amd64"
    }
}

locals {
    assetsPath = "../assets"
} 

Dockerfile:

# syntax=docker/dockerfile:1

FROM debian:stable-slim
COPY test.sh /test.sh

test.sh:

#!/bin/bash

echo "Hello world!"

Provider:

provider "docker" {
    registry_auth {
        # auth
    }
}

Config:

Docker Desktop 4.36.0 (175267) on MacOS 14.5
OpenTofu v1.8.6 on darwin_arm64
registry.opentofu.org/kreuzwerker/docker" {
  version     = "3.0.2"
}

I tried running the build manually and it worked. But then the build context is the filePath and not http://build-context-xxx. I guess the issue is coming from my setup but can't it; do you have ideas what it might be ?


Solution

  • We encountered the same issue.

    The problem is that the Docker syntax is not pinned when using the syntax comment. This worked in version 1.10. You can either pin the syntax to the last working version or remove the line completely to use the version that comes with your Docker installation.

    # syntax=docker/dockerfile:1.10
    

    According to the official docker documentation: https://docs.docker.com/build/concepts/dockerfile/#dockerfile-syntax

    We recommend using docker/dockerfile:1, which always points to the latest release of the version 1 syntax. BuildKit automatically checks for updates of the syntax before building, making sure you are using the most current version.

    And according to https://docs.docker.com/reference/dockerfile/#syntax

    If unspecified, BuildKit uses a bundled version of the Dockerfile frontend.

    This solution does not answer why the build fails using the newer syntax version, but fixes it for now.