1

I am using below two commands to connect to a remote VM host and access the docker bash.

ssh -i ~/.ssh/rsa opc@100.111.99.164
sudo docker exec -it MT bash

First I run ssh -i ~/.ssh/rsa opc@100.111.99.164 which in response connects to the specified Machine and then I run sudo docker exec -it MT bash inside the terminal of this new machine. All these I do manually and there are various such instances that I use to connect to, in a day. I want to write a script that automate this process. But the problem is, when I write the aforementioned commands in a .sh file and run it, the second command i.e sudo docker exec -it MT bash is not executed and that is expected too because the script is not running in the terminal of the machine (100.111.99.164) which I'm connected to.

I would really appreciate if someone could suggest a solution.

Uday Kumar
  • 126
  • 12
  • Have you tried putting sudo right after ssh... on the same line? – Philippe Jul 13 '20 at 07:00
  • See ["How to ssh from within a bash script?"](https://stackoverflow.com/questions/1895185/how-to-ssh-from-within-a-bash-script) – Gordon Davisson Jul 13 '20 at 10:11
  • Automation tools like Ansible, Chef, or Salt Stack are designed for this case. Not for getting debugging shells on arbitrary containers, but there's probably some higher-level administrative task you're trying to achieve. – David Maze Jul 13 '20 at 13:54

1 Answers1

0

You can pass the docker exec command to ssh command.

ssh -i ~/.ssh/rsa -tt opc@100.111.99.164 "sudo su - root -c 'docker exec -it MT bash'"  

or if you want to run command in container then you can pass the command to container


ssh -i ~/.ssh/rsa -tt opc@100.111.99.164 "sudo su - root -c 'docker exec -it MT bash  -c \"echo hello from cotainer\"'"  
Adiii
  • 54,482
  • 7
  • 145
  • 148