Environment replacement
Environment variables (declared withtheENVstatement) can also be
used in certain instructions as variables to be interpreted by the
Dockerfile. Escapes are also handled for including variable-like syntax
into a statement literally.
Environment variables are notated in the Dockerfile either with$variable_nameor${variable_name}. They are treated equivalently and the
brace syntax is typically used to address issues with variable names with no
whitespace, like${foo}_bar.
The${variable_name}syntax also supports a few of the standardbashmodifiers as specified below:
${variable:-word}indicates that ifvariableis set then the result
will be that value. Ifvariableis not set thenwordwill be the result.${variable:+word}indicates that ifvariableis set thenwordwill be
the result, otherwise the result is the empty string.
The following variable replacements are supported in a pre-release version of
Dockerfile syntax, when using the# syntax=docker/dockerfile-upstream:mastersyntax
directive in your Dockerfile:
${variable#pattern}removes the shortest match ofpatternfromvariable,
seeking from the start of the string.str=foobarbazecho${str#f*b}# arbaz${variable##pattern}removes the longest match ofpatternfromvariable,
seeking from the start of the string.str=foobarbazecho${str##f*b}# az${variable%pattern}removes the shortest match ofpatternfromvariable,
seeking backwards from the end of the string.string=foobarbazecho${string%b*}# foobar${variable%%pattern}removes the longest match ofpatternfromvariable,
seeking backwards from the end of the string.string=foobarbazecho${string%%b*}# foo${variable/pattern/replacement}replace the first occurrence ofpatterninvariablewithreplacementstring=foobarbazecho${string/ba/fo}# fooforbaz${variable//pattern/replacement}replaces all occurrences ofpatterninvariablewithreplacementstring=foobarbazecho${string//ba/fo}# fooforfoz