Open vidhi-5794 opened 1 year ago
Build-args are set as environment-variables in your RUN
instructions, and (also see the ENV
and ARG
instructions in the Dockerfile).
When interpolating them it depends on the shell what's accepted, and how these are handled. The daemon (and OCI runtime) generally accept anything, except NUL
characters; here's your earlier example, but using env
to print the environment variables
docker build --progress=plain --build-arg 'key2-temp=hello world' -<<'EOF'
FROM alpine
ARG key2-temp
RUN env
EOF
Which shows this output for the env
command;
#5 [2/2] RUN env
#5 0.150 key2-temp=hello world
#5 0.150 SHLVL=1
#5 0.150 HOME=/root
#5 0.150 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
#5 0.150 PWD=/
#5 DONE 0.2s
Or, using docker run
:
docker run --name foo --env "hello-world=hello-value" --env "hello world=hello value" --env "hello.world=hello value" alpine env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=1c5f844cc8c1
hello world=hello value
hello.world=hello value
hello-world=hello-value
HOME=/root
Or inspecting the container:
docker inspect --format '{{json .Config.Env}}' foo | jq .
[
"hello world=hello value",
"hello.world=hello value",
"hello-world=hello-value",
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
]
The POSIX specification allows for most characters (except NUL
) to be used, but recommends only using characters from the Portable Character Set; see http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html
Environment variable names used by the utilities in the Shell and Utilities volume of IEEE Std 1003.1-2001 consist solely of uppercase letters, digits, and the '_' (underscore) from the characters defined in Portable Character Set and do not begin with a digit. Other characters may be permitted by an implementation; applications shall tolerate the presence of such names. Uppercase and lowercase letters shall retain their unique identities and shall not be folded together. The name space of environment variable names containing lowercase letters is reserved for applications.
Unfortunately that also means that we cannot validate characters; we once did, but this caused issues as some tools depend on "invalid" env-vars (https://github.com/moby/moby/issues/16585), so we had to revert (https://github.com/moby/moby/pull/16608).
As my earlier example shows; these vars can be set in the environment, however, it depends on the shell how they are interpreted. In your $key2-temp
example, you're using a hyphen; shells generally don't allow for a hyphen as part of an env-var, so they consider $key2
to be an environment variable (which is not set), and -temp
to be a string following it.
Hyphens also have special meaning in most (POSIX compliant) shells; https://pubs.opengroup.org/onlinepubs/009604499/utilities/xcu_chap02.html#tag_02_06_02
So for example ${key2-temp}
is treated as;
$key2
is set, print its value$key2
is set, but null
, then don't print anything$key2
is not set, then print "word" (temp
)docker run --rm --env "hello-world=hello-value" alpine sh -c 'echo ${hello-world}'
world
So in general, I would recommend sticking to a-z A-Z 0-9
and underscores (_
) for variable names, as those would be most portable.
Maybe the docs could be improved here?
\cc @dvdksn
Description
special characters like hyphen, space is not working in docker build arg key (only under score _ works in key)
Reproduce
--build-arg 'key2-temp'='test value2' --build-arg 'key1 test'='test 1' is not working
ARG key2-temp RUN echo $key2-temp # it prints -temp2 instead of its value.
Expected behavior
it should print test value2
docker version
docker info
Additional Info
Is there any documentation which suggests that we cant use special characters including hyphen will not work in docker build arg key?