Home > OS >  Shell script not running as bash using Dockerfile
Shell script not running as bash using Dockerfile

Time:09-21

I need to split a comma-separated string into array and run k6 for each of the array values parallely. Since shell script doesn't support arrays, I m using bash command in the script. I am not able to run it as bash using Dockerfile in TeamCity.

Dockerfile:

FROM loadimpact/k6:0.34.1
COPY ./src/lib /lib
COPY ./src/scenarios /scenarios
COPY ./src/k6-run-all.sh /k6-run-all.sh
WORKDIR /
ENTRYPOINT []
RUN bash -c "./k6-run-all.sh"

Shell script:

#!/bin/bash
K6_RUN_OPTIONS=${K6_RUN_OPTIONS}
ENV_NAME=${ENV_NAME:-qa}
IS_TEST_RUN=${IS_TEST_RUN:-true}
SCENARIO_NAME=${SCENARIO_NAME:-"full-card-visa"}
GWC_PC_ID=${GWC_PC_ID}

IFS=',' read -r -a PCIds <<< "$GWC_PC_ID"
echo "Number of PC ids provided in environment variables=" ${#PCIds[@]}


if [[ ${#PCIds[@]} > 0 ]]; then
   for pcId in "$@"
   do
      ENV_NAME=$ENV_NAME RUN_OPTIONS=$SCENARIO_NAME-$ENV_NAME$OPTIONS_VARIANT GWC_PC_ID=$pcId k6 run $K6_RUN_OPTIONS ''$SCENARIO/index.js'' &
   done
fi

existCode=$?
if [ $existCode -ne 0 ]; then
   echo "Scenario $SCENARIO_NAME completed with the error"
   exit $existCode
fi

Error:

 #9 [5/6] RUN bash -c "./k6-run-all.sh"
17:02:02   #9 0.356 /bin/sh: bash: not found
17:02:02   #9 ERROR: executor failed running [/bin/sh -c bash -c "./k6-run-all.sh"]: exit code: 127
17:02:02   ------
17:02:02    > [5/6] RUN bash -c "./k6-run-all.sh":
17:02:02   #9 0.356 /bin/sh: bash: not found
17:02:02   ------
17:02:02   failed to solve: executor failed running [/bin/sh -c bash -c "./k6-run-all.sh"]: exit code: 127

How to modify Dockerfile or shell script to run this shell script as bash? Previously to run it as bash script the last line of Dockerfile used to be:

CMD ["sh", "-c", "./k6-run-all.sh"]

******* Edit: **********

Updated full script after knittl's answer (current issue is after adding & for parallel runs it is not working, it is not running anything inside the for loop and it is not giving any extra error or information in logs, it is like it is skipping it):

K6_RUN_OPTIONS=${K6_RUN_OPTIONS}
ENV_NAME=${ENV_NAME:-qa}
IS_TEST_RUN=${IS_TEST_RUN:-true}
SCENARIO_NAME=${SCENARIO_NAME:-"full-card-visa"}
GWC_PC_ID=${GWC_PC_ID}

OPTIONS_VARIANT=""
if $IS_TEST_RUN; then
   OPTIONS_VARIANT="-test"
fi

SCENARIO_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
SCENARIO_PATH="${SCENARIO_DIR}/scenarios"
SCENARIO="${SCENARIO_PATH}/${SCENARIO_NAME}"
echo "Executing scenario path $SCENARIO"

SCENARIO_NAME=${SCENARIO:${#SCENARIO_PATH} 1:${#SCENARIO}}
echo "Scenario Name: $SCENARIO_NAME"
echo "Run option: $SCENARIO_NAME-$ENV_NAME$OPTIONS_VARIANT"

echo "pc ids provided in environment variable=" $GWC_PC_ID

if [ -z "$GWC_PC_ID" ]
then
   ENV_NAME=$ENV_NAME RUN_OPTIONS=$SCENARIO_NAME-$ENV_NAME$OPTIONS_VARIANT k6 run $K6_RUN_OPTIONS ''$SCENARIO/index.js''
else
   for pcId in $(printf '%s' "$GWC_PC_ID" | tr , ' ');
   do
      ENV_NAME=$ENV_NAME RUN_OPTIONS=$SCENARIO_NAME-$ENV_NAME$OPTIONS_VARIANT GWC_PC_ID=$pcId k6 run $K6_RUN_OPTIONS ''$SCENARIO/index.js'' &
   done
fi

existCode=$?
if [ $existCode -ne 0 ]; then
   echo "Scenario $SCENARIO_NAME completed with the error"
   exit $existCode
fi

CodePudding user response:

k6 Docker containers do not come with bash preinstalled, but with busybox. I see two options:

  1. Create your own Docker image based off grafana/k6 and manually install bash in your image.

  2. Rewrite your script to not rely on bashisms. Should be fairly easy: split your list of tests to run into one path per line and while read -r path; do …; done them. Or if support for whitespace in filenames is not required, then for path in $(printf '%s' "$GWC_PC_ID" | tr , ' '); do …; done

    Note that your current script will return with the exit code of your last k6 process, meaning that if any other test failed but the last one was successfull, that will mask the error.

PS. Time to upgrade your base Docker image too. loadimpact/k6:0.34.1 is really old (exactly 1 year). It's better to switch to grafana/k6:0.40.0, which was released a week ago.

  • Related