I am currently building a container in my compose with a command
docker compose build --build-arg VAR_NAME=$(some_command) service_name
and I would like to build the same container with the simpler command docker compose build service_name, but I have not found a way to have docker compute the new output of some_command at each build.
To accomplish this I can freely change both the compose file and the Dockerfile for the container.
Is there any way to have docker instantiate a build argument or some other kind of variables dynamically on each build?
What I tried so far:
My first idea was to do something like the following
services:
service_name:
build:
context: ./service_name
args:
VAR_NAME: $(some_command)
or
VAR_NAME: "$(some_command)"
or
VAR_NAME: "$$(some_command)"
but none of them works, even if
services:
service_name:
build:
context: ./service_name
args:
VAR_NAME: 'example of some_command output'
works correctly.
build.shcontainsdocker-compose build --build-arg MY_VAR=$(command)user: $(id -u)) and notes that you cannot do command substitution or other advanced shell substitutions in a Compose file.