I am trying to run bash inside docker container using docker exec bash -c
and store its output on the host(not the container) by echo'ing something which will be used in conditional statement. I could be echo'ing anything like ip hostname or just a string. Problem is that the echo output
[root@localhost ~]# var1=$(docker exec -t test_masternode1 bash -c 'echo 1')
[root@localhost ~]# echo $var1
1
[root@localhost ~]# declare -p var1
"eclare -- var1="1
[root@localhost ~]# var2=1
[root@localhost ~]# declare -p var2
declare -- var2="1"
[root@localhost ~]# echo $var2
1
[root@localhost ~]# if [ "$var1" == "$var2" ];then echo "same";else echo "not same";fi
not same
[root@localhost ~]#
As you can see var1 and var2 both echo 1 but the if-else condition is failing. Also the variable declaration output is maligned. Something is messing up the output from docker container to host, not sure what that is
I also tried another docker image from Ubuntu. Example above was from centos7
[root@localhost ~]# var3=`docker run -t ubuntu bash -c 'echo 1'`
[root@localhost ~]# declare -p var3
"eclare -- var3="1
[root@localhost ~]#
\r
) at the end, as if it had been a line from a DOS text file. I don't know Docker so I can't say whether this is to be expected or not. – Kusalananda♦ Dec 10 '18 at 13:40docker exec -t test_masternode1 bash -c 'echo 1' | sed -e's/\r//'
– Dhirendra Khanka Dec 10 '18 at 15:09