简体   繁体   中英

bash script does not capture exit code 1 properly

I have a bash script in which I start a docker. The docker start fails due to some error which exist in there and it clearly says exit code 1 . This is the script I have to run the docker command

startContainer(){

  echo "change directory to ..."
  cd "..."

  docker-compose -f ./docker-compose.yml up -d
  if [[ $? -eq 0 ]]; then
      echo "Executed docker-compose successfully on ${HOST_APP_HOME}"
  else
    echo "Failed to start container on ${HOST_APP_HOME}. Failed command:  docker-compose -f ${DOCKER_CONF_FILE} up -d"
    printErrorFinish
  fi
}

The docker-compose command fails and it clearly prints this message

 exited with code 1

But my script does not capture it and the first condition (-eq 0) gets executed. Why it can't capture this error and consider it as a successful command?

I think the status code of the docker-compose doesn't really make sense on it's own. It is in charge of running multiple other containers, the exist status you see printed is probably from one of the containers. Base on what your docker-compose file is doing you can use --exit-code-from option to get the exit code of each service. You can also add a health-check mechanism for desired services in order to know which one is running and which one is not (a service which is deployed successfully doesn't return any value but could be checked with health check).

You can read about --exit-code-from here .

Sorry that I don't know a better way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM