79092941

Date: 2024-10-16 07:46:26
Score: 0.5
Natty:
Report link

How can I correctly capture and handle the exit status of the Docker command while still piping its output to both a file and the console?

The potential issue probably is that, when using a pipe (|) to send the output of one command to another, only the exit code of the last command in the pipeline is captured by $?, not the exit code of the original Docker command.

docker run tensorpod/pymimic3:latest \
        bash -ic "pytest tests/test_metrics" 2>&1 | tee output.txt

What you can try to capture the correct exit code of the Docker command while still piping its output to both a file and the console

name: Test Docker Command

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - name: Run test
        run: |
          docker_output=$(docker run tensorpod/pymimic3:latest \
              bash -ic "pytest tests/test_metrics" 2>&1)

          docker_exit_code=$?
          echo "$docker_output" | tee output.txt
      
          echo "Pipe status: ${PIPESTATUS[0]}"
          echo "Exit status: $docker_exit_code"

          if [ $docker_exit_code -ne 0 ]; then
            echo "The command inside Docker failed."
            exit 1
          fi
Reasons:
  • Blacklisted phrase (0.5): How can I
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Starts with a question (0.5): How can I
  • Low reputation (0.5):
Posted by: Shelton Liu