How to loop for 3 times in bash script when docker push fails?

Use for-loop and && break:

for n in {1..3}; do
    docker push $CONTAINER_IMAGE:latest && break;
done

break quits the loop, but only runs when docker push succeeded. If docker push fails, it will exit with error and the loop will continue.


You can also use :

for n in {1..4}; do
     if docker push $CONTAINER_IMAGE:latest  
     then 
          break;
     fi
done

The then statement will be entered only if the docker command succeeds.