1

Let's say I have two servers, A and B. I also have a bash script that is executed on server A that looks like this:

build_test.sh

#!/bin/bash
ssh user@B <<'ENDSSH'
echo "doing test"
bash -ex test.sh
echo "completed test"
ENDSSH

test.sh

#!/bin/bash
docker exec -i my_container /bin/bash -c "echo hi!"

The problem is that completed test does not get printed to the terminal.

Here's the output of running build_test.sh:

$ ./build_test
doing test
+ docker exec -i my_container /bin/bash -c "echo hi!"
hi!

I'm expecting completed test to be output after hi!, but it isn't. How do I fix this?

Nathan Jones
  • 4,904
  • 9
  • 44
  • 70

1 Answers1

2

docker is consuming, though not using, its standard input, which it inherits from test.sh. test.sh inherits its standard input from bash, which inherits its standard input from ssh. This means that docker itself is reading the last line of the script before the remote shell can.

To fix, just redirect docker's standard input from /dev/null.

docker exec -i my_container /bin/bash -c "echo hi!" < /dev/null
chepner
  • 497,756
  • 71
  • 530
  • 681