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 user@remote.machine). And run:

docker ps


Now lets pick a container we want to isolate (I've chosen ghostblog).

docker ps --filter name=ghostblog

We are getting closer. Lets add grep command.

docker ps --filter name=ghostblog | grep ghost

For a final touch lets add cut command.

docker ps --filter name=ghostblog | grep ghost | cut -b 104-140

Oops... Lets correct the cut -b part :)

docker ps --filter name=ghostblog | grep ghost | cut -b 94-140

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  

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

Works! Don't forget to remove "con_name".tmp file.