Skip to content

Instantly share code, notes, and snippets.

@rgschmitz1
Last active April 22, 2022 01:13
Show Gist options
  • Save rgschmitz1/1cc3f375e582433dc80191b87d2e103e to your computer and use it in GitHub Desktop.
Save rgschmitz1/1cc3f375e582433dc80191b87d2e103e to your computer and use it in GitHub Desktop.
Docker ternary operation experiment

Alpine Dockerfile

FROM python:3.7-alpine

ARG TEST_COMMAND=false
ARG TEST_ENABLED=true
# This will eval test command and fail but not stop build process because exit
# status of echo command issued after is 0 (shell scripts are weird)
RUN $TEST_ENABLED && eval "$TEST_COMMAND" || echo "skipping test"

RUN echo "This line should not be reached but shell scripts are weird"

ARG TEST_COMMAND=true
ARG TEST_ENABLED=true
# This will eval test command and continue
RUN [ "$TEST_ENABLED" = "false" ] && echo "skipping test" || eval "$TEST_COMMAND"

ARG TEST_COMMAND=true
ARG TEST_ENABLED=false
# This will echo skipping test and continue
RUN [ "$TEST_ENABLED" = "false" ] && echo "skipping test" || eval "$TEST_COMMAND"

ARG TEST_COMMAND=false
ARG TEST_ENABLED=true
# This will eval test command and exit build due to failure on the last command issued
RUN [ "$TEST_ENABLED" = "false" ] && echo "skipping test" || eval "$TEST_COMMAND"

RUN echo "This line will not be reached"

Alpine build log

Sending build context to Docker daemon  7.168kB
Step 1/15 : FROM python:3.7-alpine
 ---> 56dc68fee36a
Step 2/15 : ARG TEST_COMMAND=false
 ---> Running in de211fa1c2d6
Removing intermediate container de211fa1c2d6
 ---> ed5bac9854bd
Step 3/15 : ARG TEST_ENABLED=true
 ---> Running in c999a4888e87
Removing intermediate container c999a4888e87
 ---> a0c362210387
Step 4/15 : RUN $TEST_ENABLED && eval "$TEST_COMMAND" || echo "skipping test"
 ---> Running in 27f98332f464
skipping test
Removing intermediate container 27f98332f464
 ---> 5e4a650ff9e1
Step 5/15 : RUN echo "This line should not be reached but shell scripts are weird"
 ---> Running in 326d2348ee11
This line should not be reached but shell scripts are weird
Removing intermediate container 326d2348ee11
 ---> 53a151b471cf
Step 6/15 : ARG TEST_COMMAND=true
 ---> Running in c82366620ca6
Removing intermediate container c82366620ca6
 ---> e7ae1ecc6ec3
Step 7/15 : ARG TEST_ENABLED=true
 ---> Running in 79eda4689ae1
Removing intermediate container 79eda4689ae1
 ---> fa44fc900b13
Step 8/15 : RUN [ "$TEST_ENABLED" = "false" ] && echo "skipping test" || eval "$TEST_COMMAND"
 ---> Running in bb1e8a9b753f
Removing intermediate container bb1e8a9b753f
 ---> d6356b498eb7
Step 9/15 : ARG TEST_COMMAND=true
 ---> Running in 34bab57abdd2
Removing intermediate container 34bab57abdd2
 ---> 5aac21c7027e
Step 10/15 : ARG TEST_ENABLED=false
 ---> Running in 18ff5a03cdb0
Removing intermediate container 18ff5a03cdb0
 ---> c8f1121a41b3
Step 11/15 : RUN [ "$TEST_ENABLED" = "false" ] && echo "skipping test" || eval "$TEST_COMMAND"
 ---> Running in 7a8e26b5380a
skipping test
Removing intermediate container 7a8e26b5380a
 ---> 5e43c9bc5054
Step 12/15 : ARG TEST_COMMAND=false
 ---> Running in 9d731ff4ed53
Removing intermediate container 9d731ff4ed53
 ---> 5b73c364c92f
Step 13/15 : ARG TEST_ENABLED=true
 ---> Running in 42640257adfa
Removing intermediate container 42640257adfa
 ---> 8b285904be7d
Step 14/15 : RUN [ "$TEST_ENABLED" = "false" ] && echo "skipping test" || eval "$TEST_COMMAND"
 ---> Running in 3821450840a9
The command '/bin/sh -c [ "$TEST_ENABLED" = "false" ] && echo "skipping test" || eval "$TEST_COMMAND"' returned a non-zero code: 1

Debian Dockerfile

FROM python:3.7-slim-buster

ARG TEST_COMMAND=false
ARG TEST_ENABLED=true
# This will eval test command and fail but not stop build process because exit
# status of echo command issued after is 0 (shell scripts are weird)
RUN $TEST_ENABLED && eval "$TEST_COMMAND" || echo "skipping test"

ARG TEST_COMMAND=true
ARG TEST_ENABLED=true
# This will eval test command and continue
RUN [ "$TEST_ENABLED" = "false" ] && echo "skipping test" || eval "$TEST_COMMAND"

ARG TEST_COMMAND=true
ARG TEST_ENABLED=false
# This will echo skipping test and continue
RUN [ "$TEST_ENABLED" = "false" ] && echo "skipping test" || eval "$TEST_COMMAND"

ARG TEST_COMMAND=false
ARG TEST_ENABLED=true
# This will eval test command and exit build due to failure on the last command issued
RUN [ "$TEST_ENABLED" = "false" ] && echo "skipping test" || eval "$TEST_COMMAND"

RUN echo "This line will not be reached"

Debian build log

Sending build context to Docker daemon  10.24kB
Step 1/15 : FROM python:3.7-slim-buster
 ---> 1bf4bc889e42
Step 2/15 : ARG TEST_COMMAND=false
 ---> Running in 50143f5255e9
Removing intermediate container 50143f5255e9
 ---> d47e8f2488ec
Step 3/15 : ARG TEST_ENABLED=true
 ---> Running in 1aff6d3655f8
Removing intermediate container 1aff6d3655f8
 ---> 122803443625
Step 4/15 : RUN $TEST_ENABLED && eval "$TEST_COMMAND" || echo "skipping test"
 ---> Running in 69c15f1092c3
skipping test
Removing intermediate container 69c15f1092c3
 ---> 32792e35d257
Step 5/15 : RUN echo "This line should not be reached but shell scripts are weird"
 ---> Running in 7908c3a7974d
This line should not be reached but shell scripts are weird
Removing intermediate container 7908c3a7974d
 ---> 75b4d9bbaf66
Step 6/15 : ARG TEST_COMMAND=true
 ---> Running in c7a0df9334f3
Removing intermediate container c7a0df9334f3
 ---> d0845325024b
Step 7/15 : ARG TEST_ENABLED=true
 ---> Running in 778151d1f7b4
Removing intermediate container 778151d1f7b4
 ---> ca9b4087eab2
Step 8/15 : RUN [ "$TEST_ENABLED" = "false" ] && echo "skipping test" || eval "$TEST_COMMAND"
 ---> Running in 9714c330dc9e
Removing intermediate container 9714c330dc9e
 ---> aeda355a2caa
Step 9/15 : ARG TEST_COMMAND=true
 ---> Running in ebb518c597ef
Removing intermediate container ebb518c597ef
 ---> 784ee5ce24d9
Step 10/15 : ARG TEST_ENABLED=false
 ---> Running in ffbbba5d7c1c
Removing intermediate container ffbbba5d7c1c
 ---> ffa8563733a5
Step 11/15 : RUN [ "$TEST_ENABLED" = "false" ] && echo "skipping test" || eval "$TEST_COMMAND"
 ---> Running in 0c5bc447fac5
skipping test
Removing intermediate container 0c5bc447fac5
 ---> b4434d773288
Step 12/15 : ARG TEST_COMMAND=false
 ---> Running in 765c59c5cf53
Removing intermediate container 765c59c5cf53
 ---> 5e861c57b25f
Step 13/15 : ARG TEST_ENABLED=true
 ---> Running in e26461593dd1
Removing intermediate container e26461593dd1
 ---> 61269a23fa21
Step 14/15 : RUN [ "$TEST_ENABLED" = "false" ] && echo "skipping test" || eval "$TEST_COMMAND"
 ---> Running in 156ba2a4671f
The command '/bin/sh -c [ "$TEST_ENABLED" = "false" ] && echo "skipping test" || eval "$TEST_COMMAND"' returned a non-zero code: 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment