1

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

ssh -i ~/.ssh/rsa [email protected]
sudo docker exec -it MT bash

First I run ssh -i ~/.ssh/rsa [email protected] 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.

3
  • Have you tried putting sudo right after ssh... on the same line? Commented Jul 13, 2020 at 7:00
  • See "How to ssh from within a bash script?" Commented Jul 13, 2020 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. Commented Jul 13, 2020 at 13:54

1 Answer 1

0

You can pass the docker exec command to ssh command.

ssh -i ~/.ssh/rsa -tt [email protected] "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 [email protected] "sudo su - root -c 'docker exec -it MT bash  -c \"echo hello from cotainer\"'"  
Sign up to request clarification or add additional context in comments.

2 Comments

what command did you try to execute inside the container? and btw the answer was supposed to above issue ;)
I tried to use cd command. cd /logs/ETL/app

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.