hosselausso
hosselausso

Reputation: 1087

Docker build time arguments

I have a bunch of Dockerfiles that are build from a common automated place using the same build command:

docker build -t $name:$tag --build-arg BRANCH=$branch .

Some of the Dockerfiles contain this:

ARG BRANCH=master

And that argument is used for some steps of the image build.

But for some Dockerfiles which doesn't need that argument I get this error at the end:

One or more build-args [BRANCH] were not consumed, failing build.

How can I overcome this problem without including the argument to all the Dockerfiles?

Upvotes: 2

Views: 2820

Answers (2)

BMitch
BMitch

Reputation: 264761

I don't see any documented way to avoid this error without changing your input or your Dockerfile. robertobado already covers changing your input. As a second option, you can include an effectively unused build arg at the end of your Dockerfile which would have a very minor impact on your build.

ARG BRANCH=undefined
RUN echo "Built from branch ${BRANCH}"

Since this doesn't modify the filesystem, I believe the image checksum will be identical.

Upvotes: 0

robertobado
robertobado

Reputation: 93

Have you considered grepping your Dockerfile for BRANCH and using it result to decide if you should supply your ARG or not?

You could replace your automation build trigger with something like:

if grep BRANCH Dockerfile; then docker build -t $name:$tag --build-arg BRANCH=$branch .; else docker build -t $name:$tag . ; fi

Upvotes: 3

Related Questions