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.
2
Answers
@tom's answer is likely better but out of stubbornness I decided to generate a
docker-compose.ovverride.yml
with the correct values.I went for ad-hoc template processed with
sed
:docker-compose.template
:and a simple sed script:
This is quite ugly but works for my use-case
TODO:
sed
's regex pattern, currently the output is in the pattern[0-9]+
so it is fineit seems that docker does not support running shell commands in a
docker-compose.yml
see the Using shell command in docker-compose.yml github discussion
you could use a script to wrap your build command.