1

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?

8
  • Do you know that it's docker-compose itself that's exiting with status 1, and not one of its subprocesses? (To be clear, the above is rhetorical; I'm asserting that you don't). Commented Mar 13, 2020 at 23:56
  • The first thing I'd do is stop using $?; it's more robust to write your code as if docker-compose -f ./docker-compose.yml up -d; then, just so there can't possibly be a DEBUG trap or other thing going on behind your back that changes the exit status before it's captured. But it's more likely that the error message comes from a deeper layer and isn't being relayed back, so the place I'd go past that is using sysdig to track all the individual processes involved and the exit status of each (and to see exactly which process it is that writes the "exited with code 1" message). Commented Mar 13, 2020 at 23:57
  • (...that, and whether that process actually exits with status 1; and if it does, who its parent is and whether that parent actually exits with status 1, etc. until control is returned to the shell script). Commented Mar 13, 2020 at 23:59
  • how can I figure out where error code 1 is coming from docker-compose command or another process? Commented Mar 14, 2020 at 0:09
  • Since you're executing the if block, it's clearly coming from something else. Presumably something docker-compose executes internally. Commented Mar 14, 2020 at 0:52

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Is it compatible with -d option?
@HHH the option I mentioned, no, but health checking after a while is

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.