2

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:

  • statically, ie hardcoded in docker-compose file.
  • dynamically, ie passed to docker-compose file itself.
Maths noob
  • 1,684
  • 20
  • 42
  • very basically => `services: {my_service: {build: {context: ${CONTEXT_FROM_ENV}}}}`. See https://docs.docker.com/compose/compose-file/build/#illustrative-sample and https://docs.docker.com/compose/environment-variables/ – Zeitounator Nov 04 '22 at 11:27
  • note that i want to pass a named context, not the default one. in principle there are multiple named contexts I might want to pass. – Maths noob Nov 04 '22 at 11:29
  • see added code snippet which should help clarify the setup. – Maths noob Nov 04 '22 at 11:46
  • See the first documentation link above. It does not look like this is taken in charge yet by docker-compose specification. – Zeitounator Nov 04 '22 at 12:31
  • did you read this: https://docs.docker.com/build/customize/bake/compose-file/#extension-field-with-x-bake – Lety Nov 16 '22 at 10:29

1 Answers1

4

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.

Maths noob
  • 1,684
  • 20
  • 42