I have Docker running on an AWS EC2 instance (Elastic Beanstalk). I'd like to interactively run bash in the container automatically with a script I run from my shell without going through typing all the commands every time.
I made this using a superuser answer about printing individual arguments based on regex. If I type or paste it while logged into AWS it runs fine.
sudo docker exec -it $(sudo docker ps | awk '{for(i=1;i<=NF;i++){if($i~/ecs-awseb-mything-.*/){print $i}}}') bash
This is my SSH connect "script":
#!/usr/bin/env bash
ssh -i /path/to/my.pem awsuser@myhost
I tried combining the two with no avail using ssh -t:
#!/usr/bin/env bash
ssh -i /path/to/my.pem awsuser@myhost -t "sudo docker exec -it $(sudo docker ps | awk '{for(i=1;i<=NF;i++){if($i~/ecs-awseb-mything-.*/){print $i}}}') bash"
But no luck. Here's the transcript of console session:
myuser@MYCOMPUTER:~$ ./myscript.sh
sudo: unable to resolve host MYCOMPUTER
[sudo] password for myuser:
myuser@MYCOMPUTER:~$
It seems like it's trying to execute the command and THEN connect? So I swapped it to this:
#!/usr/bin/env bash
ssh -i /path/to/my.pem -t "sudo docker exec -it $(sudo docker ps | awk '{for(i=1;i<=NF;i++){if($i~/ecs-awseb-mything-.*/){print $i}}}') bash" awsuser@myhost
Still the exact same issue. Thanks for help!