1

I write a bash shell to detemine docker-compose up or docker-compose restart:

#!/bin/bash
re=$(docker-compose -f prod.yml restart)
echo re:${re}
if [[ -n ${re} && ${re} == *"No containers to restart"* ]];then
  echo -e '\e[0;31;1mNO CONTAINER FOUND. WILL EXEC UP COMMAND...\e[0m'
  docker-compose -f prod.yml up
fi

but each time I exec this script, the var re is always empty. The output of script is:

ERROR: No containers to restart
re:
  • ERROR: No containers to restart is the output of docker-compose -f prod.yml restart

Is there any way to solve this issue or another way to achieve my goal ?

2
  • Add output of docker-compose -f prod.yml restart to your question. Commented Jul 21, 2018 at 6:06
  • 1
    Checking for a particular output string is brittle and error-prone. If Docker is at all correctly written, it will set its exit code to reflect whether it succeeded. Then you can simply say if docker-compose -f prod.yml restart; then... Commented Jul 21, 2018 at 6:52

1 Answer 1

3

You can try to redirect stderr output. For example: re=$(docker-compose -f prod.yml restart 2>&1)

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

Comments

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.