buildx icon indicating copy to clipboard operation
buildx copied to clipboard

bake: unclear path resolution when setting both dockerfile and context for a target

Open nickawilliams opened this issue 1 year ago • 2 comments

Contributing guidelines

I've found a bug and checked that ...

  • [x] ... the documentation does not mention anything about my problem
  • [x] ... there are no open or closed issues that are related to my problem

Description

I’ve encountered what seems to be unexpected behavior regarding the dockerfile and context target attributes in a docker-bake.hcl file.

  • When dockerfile is set to a path relative to the docker-bake.hcl file, everything works as expected, and the build succeeds.

  • However, when the context attribute is set to a subdirectory, the build fails with the following error:

    ERROR: failed to solve: failed to read dockerfile: open Dockerfile: no such file or directory
    

It appears that the dockerfile path is treated as relative to the context directory, not the docker-bake.hcl file. While this behavior is logical in isolation, it is inconsistent with how paths are handled in other tools like docker-compose.yaml, where both attributes are interpreted as relative to the file defining them.

Expected behaviour

Both dockerfile and context (and by extension contexts) should consistently resolve relative paths from the docker-bake.hcl file to maintain consistency across the toolset.

Alternatively the docs could be updated to explicitly explain the relationship between these attributes.

Actual behaviour

The dockerfile path is unexpectedly relative to the context, causing the build to fail when context is set.

Buildx version

github.com/docker/buildx v0.17.1-desktop.1 1a2ee7e8d945b88fdc18f1948da491c2f540dec9

Docker info


Builders list

NAME/NODE           DRIVER/ENDPOINT     STATUS    BUILDKIT   PLATFORMS
default             docker
 \_ default          \_ default         running   v0.16.0    linux/arm64, linux/amd64, linux/amd64/v2, linux/riscv64, linux/ppc64le, linux/s390x, linux/386, linux/mips64le, linux/mips64
desktop-linux*      docker
 \_ desktop-linux    \_ desktop-linux   running   v0.16.0    linux/arm64, linux/amd64, linux/amd64/v2, linux/riscv64, linux/ppc64le, linux/s390x, linux/386, linux/mips64le, linux/mips64

Configuration

This issue arose while working in a monorepo with multiple Lambda functions sharing a single Dockerfile. Here’s an example structure:

./
├── lambdas/
│   ├── some_lambda_function/
│   │   └── ...source-files
│   └── some_other_lambda_function/
│       └── ...source-files
├── docker-bake.hcl
└── Dockerfile

Example docker-bake.hcl

group "default" {
  targets = ["some_lambda_function", "some_other_lambda_function"]
  flags = ["--provenance=false"]
}

target "some_lambda_function" {
  dockerfile = "Dockerfile"
  context = "lambdas/some_lambda_function"
  tags = [ "latest" ]
}

target "some_other_lambda_function" {
  dockerfile = "Dockerfile"
  context = "lambdas/some_other_lambda_function"
  tags = [ "latest" ]
}

Build logs


Additional info

No response

nickawilliams avatar Dec 04 '24 05:12 nickawilliams

I have a similar problem. When building everything is fine, but when I try to change package.json and bake starts to rebuild the container, I get errors. There seems to be some kind of relative paths problem on Windows

Build failed. Error: failed to evaluate path "C:\\Users\\mrkir\\PhpstormProjects\\simplevk-doc/C:\\Users\\mrkir\\PhpstormProjects\\simplevk-doc\\Dockerfile.dev": CreateFile C:\Users\mrkir\PhpstormProjects\simplevk-doc\C:: The filename, directory name, or volume label syntax is incorrect

msg="Error handling changed files: failed to evaluate path \"C:\\\\Users\\\\mrkir\\\\PhpstormProjects\\\\simplevk-doc/C:\\\\Users\\\\mrkir\\\\PhpstormProjects\\\\simplevk-doc\\\\Dockerfile.dev\": CreateFile C:\\Users\\mrkir\\PhpstormProjects\\simplevk-doc\\C:: The filename, directory name, or volume label syntax is incorrect.

Platdorm: Windows 11 Docker Compose version v2.33.1-desktop.1 github.com/docker/buildx v0.21.1-desktop.2 65a04767fd3000fbac3b7120cc6115d518678e33

docker compose up --watch vuepress-dev

# .env
COMPOSE_BAKE=true
services:
  vuepress-dev:
    build:
      context: .
      dockerfile: ./Dockerfile.dev
    container_name: vuepress-dev
    ports:
      - "8080:8080"
    develop:
      watch:
        - action: sync
          path: ./docs
          target: /app/docs
          ignore:
            - node_modules/
            - .vuepress/dist/
            - .vuepress/.temp/
            - .vuepress/.cache/
        - action: rebuild
          path: ./package.json
    command: pnpm run docs:dev
    environment:
      - NODE_ENV=development

Runnin4ik avatar Mar 14 '25 15:03 Runnin4ik

I can confirm there is an issue with path concatenation in Bake on Windows 11.

Using a docker-compose.yml like:

  my-app:
    container_name: my-app
    image: my-app
    build:
      context: ./src/
      dockerfile: ./my-app/Dockerfile

I get an error like this:

failed to evaluate path "D:\\my-project\\src\\my-app/D:\\my-project\\src\\my-app\\Dockerfile": CreateFile D:\my-project\src\my-app\D:: The filename, directory name, or volume label syntax is incorrect.

Meanwhile, when running docker compose build without the COMPOSE_BUILD environment variable set to true, everything works fine.

I suppose it's worth pointing out the Docker Compose version:

PS>docker-compose --version Docker Compose version v2.33.1-desktop.1

I also tried it with:

PS>docker-compose --version Docker Compose version v2.34.0-desktop.1

vdachev-david avatar Apr 04 '25 09:04 vdachev-david