Search code examples
bashdockerdocker-build

stop docker build in case of WARN level log messages


I am building multiple Docker images from a bash script and I would like to stop the process if a WARN level build message appears.

As I see the exit code of the docker build is zero despite I have WARN level messages.

docker build \
  --no-cache \
  --build-arg BUILD_TYPE="$BUILD_TYPE" \
  ...
  --tag "$IMAGE_NAME":"$IMAGE_TAG" \
  --progress plain \
  "$IMAGE_SRC"

EXIT_CODE=$?
echo "$EXIT_CODE"

Result: I have two warnings and a zero exit code:

#1 WARN: InvalidDefaultArgInFrom: Default value for ARG ${IMAGE_FROM} results in empty or invalid base image name (line 11)
#1 WARN: InvalidDefaultArgInFrom: Default value for ARG base-$BUILD_TYPE results in empty or invalid base image name (line 56)
...
0

They are simple issues, easy to fix based on this doc.

But if the build process is not stopped then nobody will scroll up in the log at the end of the builds to search for WARN messages and potential bugs will not be fixed.

Is there any simple way to get info about WARN build messages during or at the end of the image build?


Solution

  • This (untested) should get you all the WARN messages after the build completes:

    docker build \
      --no-cache \
      --build-arg BUILD_TYPE="$BUILD_TYPE" \
      ...
      --tag "$IMAGE_NAME":"$IMAGE_TAG" \
      --progress plain \
      "$IMAGE_SRC" 2>&1 | tee docker_build_out.txt
    
    EXIT_CODE=$?
    
    grep 'WARN' docker_build_out.txt >&2
    
    echo "$EXIT_CODE"
    

    As for stopping the build when a WARN appears - generally the difference between a warning and an error in any tool is that a warning doesn't stop whatever is happening while an error does, so stopping on a warning would be unusual. If you really wanted to do it you could have a background process tailing the build output file I added above watching for WARN appearing and, if it did, kill the main build process above but I wouldn't recommend it as then you'll have to find and fix every WARN one at a time, rerunning the build command after every fix to get the next WARN and so on.