1

I'm trying to use a --build-arg in one of my run statements, but it is failing.

Dockerfile

ARG OS_VERSION
...
RUN ["node", "./bin/installtoolchain.js", "${OS_VERSION}"]

I build the image using the following command:

docker build --tag installer --build-arg OS_VERSION=9 .

The script is failing, because it is passing the variable name instead of the value.

Error: Invalid toolchain configuration settings! Received: {
    "firmwareVersion": "${OS_VERSION}"
}
...

Why isn't my --build-arg being utilized?

Zak
  • 12,213
  • 21
  • 59
  • 105
  • In the Dockerfile reference, I found that the [exec form of the RUN instruction](https://docs.docker.com/engine/reference/builder/#run) does not execute the shell, which is expected. But it does not mention that there is no way to expand the args without a shell, which is surprising! When I tried not including the quotes around variable expansion (`RUN ["node", "./bin/installtoolchain.js", ${OS_VERSION}]`), it failed to be parsed as the exec form. It was treated as the shell form, producing a confusing error message. – Palec Oct 31 '22 at 15:18

1 Answers1

2

Docker is failing to expand your argument by design.

Loosely speaking, Docker treats --build-args as temporary environment variables. Using an environment variable with the syntax ${VARIABLE} requires shell expansion.

You have explicitly bypassed the shell, by using the exec form of the RUN command.

If you wish to utilize the --build-arg, then you must use the shell form of the RUN command.

RUN node ./bin/installtoolchain.js ${OS_VERSION}

or, to be precise, invoke the command from a shell.

RUN ["bash", "-c", "node ./bin/installtoolchain.js ${OS_VERSION}"]
Zak
  • 12,213
  • 21
  • 59
  • 105
  • Adding double quotes around `${OS_VERSION}` may be a good idea. The variable is designed not to contain any special characters but better be sure its contents is passed as is, without interpretation by the shell. – Palec Oct 31 '22 at 15:12
  • So the expansion of args is really the feature of a shell and Docker has no direct support for that? In the [ARG variables usage reference](https://docs.docker.com/engine/reference/builder/#using-arg-variables), the relation to environment variables is discussed, but all the examples use the shell form and the exec form is not discussed at all. :-( – Palec Oct 31 '22 at 15:23