2

I am attempting to write some configuration based on the version I am installing in a image. I am fetching the version as a build argument.

...
ARG XDEBUG_VERSION=3.1.2
RUN if [ "$XDEBUG_VERSION" = "3.1.2" ]; then echo "yes, it is 3.1.2"; fi
RUN if [ "$XDEBUG_VERSION" = "3*" ]; then echo "yes, starts with 3"; fi
...

Checking if the value is 3.1.2 works, but checking if it starts with 3 is not working. Below is the run outupt.

Step 20/22 : RUN if [ "$XDEBUG_VERSION" = "3.1.2" ]; then echo "yes, it is 3.1.2"; fi
 ---> Running in e1499d0ec491
yes, it is 3.1.2
Removing intermediate container e1499d0ec491
 ---> ad2439fcec2e
Step 21/22 : RUN if [ "$XDEBUG_VERSION" = "3*" ]; then echo "yes, starts with 3"; fi
 ---> Running in b8da583b4522
Removing intermediate container b8da583b4522
 ---> a9dbc5535002

So, How can I check if the build argument starts with certain characters?

Starx
  • 77,474
  • 47
  • 185
  • 261
  • A Dockerfile `RUN` instruction (normally) runs a Bourne shell command; the linked question describes both a short way to do it if you're using GNU Bash, and a longer syntax that works with POSIX shells (including Alpine-based images). – David Maze Sep 30 '22 at 11:08
  • @DavidMaze Thanks but I seem to be struggling to find one that works. Also, are you saying that the behaviour of the Docker's `RUN` changes based on the system or shell it is being executed from? – Starx Oct 03 '22 at 13:39
  • You can change the `SHELL` that gets used, and on some base images `/bin/sh` is actually GNU bash. I'd stick to POSIX shell syntax if it's at all an option (and if you ever might want to use an Alpine-based image that doesn't normally have bash). – David Maze Oct 03 '22 at 14:12
  • @DavidMaze I am on Ubuntu Desktop, How can I change the shell to use `bash` because bash should have no problem interpreting what I am trying above. – Starx Oct 05 '22 at 15:40

1 Answers1

0

You can do it e.g. with the help of regex as follows:

FROM alpine:latest
...
ARG XDEBUG_VERSION=3.1.2
RUN if [ "$XDEBUG_VERSION" = "3.1.2" ]; then echo "yes, it is 3.1.2"; fi
RUN if [[ "$XDEBUG_VERSION" =~ "^3" ]]; then echo "yes, starts with 3"; fi
...

Test output with alpine:latest:

docker build --build-arg XDEBUG_VERSION=3.1.2 --progress=plain --no-cache -t string-starts .

#6 [3/3] RUN if [[ "3.1.2" =~ "^3" ]]; then echo "yes, starts with 3"; fi
#6 sha256:7921d7ff89a349bcdb49a0c023101acc5661f87e827cd53a9c7fdb645e0c50f5
#6 0.480 yes, starts with 3
#6 DONE 0.5s

#...

docker build --build-arg XDEBUG_VERSION=4.3.2 --progress=plain --no-cache -t string-starts .

#6 [3/3] RUN if [[ "4.3.2" =~ "^3" ]]; then echo "yes, starts with 3"; fi
#6 sha256:42a1802ce3edbd3f2aa31aa4254cb5216c06f0bb7de75f65b6d0ebba01923e3c
#6 DONE 0.5s
m19v
  • 1,800
  • 4
  • 11
  • 26
  • `string =~ regex` doesn't seem to be a valid option for [POSIX test(1)](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html#tag_20_128), and this answer might not work on Alpine-based images that don't have the extended GNU tool set. – David Maze Sep 30 '22 at 11:06
  • The result of test comes from a test with ```alpine:latest```, i.e. it works at least with alpine docker image. I have adjusted my answer by adding alpine as base image used in test. – m19v Sep 30 '22 at 11:09
  • @DavidMaze Can you please explain me what do you mean by "does not seem to be a valid option for Posix"? [Here](https://www.gnu.org/software/bash/manual/html_node/Conditional-Constructs.html#index-_005b_005b) one can read that it can be used for matching patterns for Posix as well etc. E.g. a statement from link: " When you use ‘=~’, the string to the right of the operator is considered a POSIX extended regular expression pattern and matched accordingly (using the POSIX regcomp and regexec interfaces usually described in regex(3))." – m19v Sep 30 '22 at 12:29
  • 2
    You're quoting the GNU Bash manual. "POSIX regular expressions" are a specific thing (the syntax used by classic grep(1)); even if the pattern match uses POSIX REs it doesn't mean POSIX shells accept the `=~` syntax. `[[ ... ]]` is also explicitly undefined in the POSIX [Shell Command Language](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_04). – David Maze Sep 30 '22 at 13:06