How To Run Docker Exec Commnds Remotely Via Ssh
It may seem a bit tricky at first glance. First we need a container name. Here is how we can get it.
Ssh to remote machine (ssh [email protected]). And run:
docker ps
![](https://blog.kafana.dev/content/images/2021/10/image-2.png)
![](https://blog.kafana.dev/content/images/2021/10/image-18.png)
Now lets pick a container we want to isolate (I've chosen ghostblog).
docker ps --filter name=ghostblog
![](https://blog.kafana.dev/content/images/2021/10/image-17.png)
We are getting closer. Lets add grep command.
docker ps --filter name=ghostblog | grep ghost
![](https://blog.kafana.dev/content/images/2021/10/image-22.png)
For a final touch lets add cut command.
docker ps --filter name=ghostblog | grep ghost | cut -b 104-140
![](https://blog.kafana.dev/content/images/2021/10/image-23.png)
Oops... Lets correct the cut -b part :)
docker ps --filter name=ghostblog | grep ghost | cut -b 94-140
![](https://blog.kafana.dev/content/images/2021/10/image-24.png)
Got it! The hardest part is over. Logout from the server and execute docker commands from your machine using command above.
docker ps --filter name=ghostblog | grep ghost | cut -b 94-140
Write it down.
We just need one more prep command.
ssh user@server-IP docker ps --filter name=ghostblog | grep ghost | cut -b 94-140 > con_name.tmp
![](https://blog.kafana.dev/content/images/2021/10/image-25.png)
We created a file on our local machine that has our "filter command".
ls if you don't believe me. :)
ssh user@server-IP docker exec -i $(cat con_name.tmp) ls -lah
![](https://blog.kafana.dev/content/images/2021/10/image-26.png)
Works! Don't forget to remove "con_name".tmp file.