Home Explore Blog CI



docker

2nd chunk of `content/manuals/build/bake/remote-definition.md`
ab0d27311212653b951ed00363b9ee5bccd5961acb7d7b8d00000001000007e8
#8 0.102 -rw-r--r--    1 root     root             0 Jul 27 18:47 bar
#8 0.102 -rw-r--r--    1 root     root             0 Jul 27 18:47 foo
#8 0.102 /bin/sh: stop: not found
```

You can append a path to the `cwd://` prefix if you want to use a specific
local directory as a context. Note that if you do specify a path, it must be
within the working directory where the command gets executed. If you use an
absolute path, or a relative path leading outside of the working directory,
Bake will throw an error.

### Local named contexts

You can also use the `cwd://` prefix to define local directories in the Bake
execution context as named contexts.

The following example defines the `docs` context as `./src/docs/content`,
relative to the current working directory where Bake is run as a named context.

```hcl {title=docker-bake.hcl}
target "default" {
  contexts = {
    docs = "cwd://src/docs/content"
  }
  dockerfile = "Dockerfile"
}
```

By contrast, if you omit the `cwd://` prefix, the path would be resolved
relative to the build context.

## Specify the Bake definition to use

When loading a Bake file from a remote Git repository, if the repository
contains more than one Bake file, you can specify which Bake definition to use
with the `--file` or `-f` flag:

```console
docker buildx bake -f bake.hcl "https://github.com/crazy-max/buildx.git#remote-with-local"
```

```text
...
#4 [2/2] RUN echo "hello world"
#4 0.270 hello world
#4 DONE 0.3s
```

## Combine local and remote Bake definitions

You can also combine remote definitions with local ones using the `cwd://`
prefix with `-f`.

Given the following local Bake definition in the current working directory:

```hcl
# local.hcl
target "default" {
  args = {
    HELLO = "foo"
  }
}
```

The following example uses `-f` to specify two Bake definitions:

- `-f bake.hcl`: this definition is loaded relative to the Git URL.
- `-f cwd://local.hcl`: this definition is loaded relative to the current
  working directory where the Bake command is executed.

Title: Specifying and Combining Bake Definitions
Summary
You can specify a path after `cwd://` to use a specific local directory as context in a remote Bake file, but it must be within the working directory. Using the `cwd://` prefix also allows defining local directories as named contexts. When a Git repository contains multiple Bake files, you can specify the one to use with the `--file` or `-f` flag. You can also combine remote and local definitions using the `cwd://` prefix with `-f`.