Maths noob
Maths noob

Reputation: 1802

Docker compose: pass named contexts to docker file

I have a docker file that using multiple build contexts copies a file from a named context: foo. ie, something like:

COPY --from=foo . /bar

I can build it like this:

docker buildx build --build-context foo=/my/dir/

I'd like to be able to pass this named context from a docker-compose file:

Upvotes: 3

Views: 841

Answers (2)

jehon
jehon

Reputation: 1668

Docker Compose version 2.17 added support in March 2023 for additional_contexts: for this use case:

build:
  context: .
  additional_contexts:
    - foo=/my/dir/

Upvotes: 1

Maths noob
Maths noob

Reputation: 1802

I managed to find a partial answer:

The way to pass named contexts to a docker file using the multi-stage semantics is as below:

target "target1" {
  context = "../path/to/docker/file"
  contexts = {
    foo = "../path/to/additional/source/contexts"
  }
}

we can even pass the context resulting from another build target target2 like this: "target:target2"

The above bake file can be baked using docker buildx bake

I'm still not clear however how the above can be used in a docker compose file to actually run an image, as opposed to just build it.

Upvotes: 4

Related Questions