title: Run an arbitrary command in a docker container that runs on a remote host after sourcing some environment variables from another command
body: <p>To show what I am trying to do, this is part of the bash script I have so far:</p>
  
  <pre><code>COMMAND="${@:1}"
  CONTAINER_DOCKER_NAME=this-value-is-computed-prior
  MY_IP=this-ip-is-computed-prior
  
  ssh user@$MY_IP -t 'bash -c "docker exec -it $( docker ps -a -q -f name='$CONTAINER_DOCKER_NAME' | head -n 1 ) /bin/sh -c "eval $(echo export FOO=$BAR) &amp;&amp; $COMMAND""'
  </code></pre>
  
  <p>So let's break down the long command:<br>
  I am ssh-ing into a host where I run a bash which fetches the correct container with <code>docker ps</code> and then I do <code>docker exec</code> to run a shell in the container to load some environment variables that my <code>$COMMAND</code> needs to work. Important to note is that <code>$BAR</code> should be the value of the <code>BAR</code> variable <strong>inside the container</strong>.<br>
  So thats what I'm trying to accomplish in theory. However when running this no matter how I set the braces, quotes or escape characters - I always run into problems, either the shell syntax is not correct or it does not run the correct command (especially when the command has multiple arguments) or it loads <code>$BAR</code> value from my local desktop or the remote host but not from the container. </p>
  
  <p>Is this even possible at all with a single shell one-liner?</p>