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?
docker-composeitself 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).$?; it's more robust to write your code asif 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 usingsysdigto 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).ifblock, it's clearly coming from something else. Presumably somethingdocker-composeexecutes internally.