Allow specifying build arguments for up sub-command
Description
Overview
By default, docker compose up invocation builds images for the requested services if these aren't present already. This is particularly useful for the CI use case, as Compose will figure out all the dependent services and build images just for those needed.
Unfortunately, up sub-command doesn't support specifying --build-arg (build-time variables) unlike build. This means that one can no longer rely on the utility of up sub-command if they would like to specify build-time variables (e.g., to embed version of software, commit SHA and so on).
One could work around this gap by:
- Invoking
docker compose buildto build all images. However, it's unideal as this'll build images that aren't used byupas well. - Invoking
docker compose build <service> <service> ...to carefully build only images that are needed. However, this is error-prone and duplicates the dependency definition between Compose invocation and the actual Compose model.
Desired behaviour
It would be great if one could invoke Compose with something like docker compose up --build-arg ... <service> <service> ... in order to build only the necessary images with the right configuration and bring up the requested services after.
Alternatives
Alternatively, one could offer a flag for docker compose build to build images for dependent services in addition to the requested service. Whilst it means an extra compose invocation like so docker compose build --dependencies myservice && docker compose up myservice, that would still fit the bill.
Please let me know what you think regarding proposed UX, I would also be happy to contribute an implementation for either of the options.
I'm not in favor for more build flags added to the (already complex) up command.
Considering the CI use-case, why not just rely on build-args set in compose.yaml, and set environment variables accordingly so that those are automatically set from env ?
services:
foo:
build:
context: path
args:
- FOO #will be set if user's environment has `FOO` variable defined
Definitely appreciate your point regarding up already being quite a complex command. I think it would still be useful to support this use case though - possibly through other means?
build.args works as a stop gap, but requires that repo owners set this up manually on every single Compose file they have. This requires quite a bit of work and boilerplate, especially when the image builds are otherwise standardised (e.g., through a custom BuildKit frontend). It would be really helpful if one could holistically inject build arguments for such setups, which is why I looked towards --build-arg.
I can also provide another use case.
If at build time secrets need to be passed (e.g. for access to a custom registry) those should not be specified in the docker compose file but rather passed on command line via --build-arg
as explained on https://github.com/docker/compose/issues/11012#issuecomment-1725009089, up command is already overloaded with too many flags. No way it will reproduce the many options available to docker build, while it's trivial to just run build then up.
I'm closing this issue