'Change RUN behaviour based on command line parameter in docker

Can I pass parameters to a RUN instruction from the command line when doing docker build?

My image is building my application using a build system, the purpose would be to be able to select different build target from the command line. Otherwise I would need a separate Dockerfile for each desired build target which is not really maintainable.

I feel like the caching system could be in the way of this feature, although it seems quite doable.

As an example, the instruction would look like:

RUN bazel build $BUILDOPTS //my/app:$TARGET

I've looked into passing args with --build-args but didn't succeed with it.



Solution 1:[1]

You should add ARG to your Dockerfile.

ARG BUILDOPTS
ARG TARGET
RUN bazel build $BUILDOPTS //my/app:$TARGET

Read more: https://docs.docker.com/engine/reference/builder/#arg

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ron van der Heijden