Using identity file in docker context via flag

Hi!

I hit a wall with this. Is there any way I can add more arguments to the ssh command that ends up being called by docker when running something like docker --context myserver container ls? I don’t like the idea of modifying my ~/.ssh/config because the IPs I’m connecting to change constantly.

It is not possible, but you can override the context with using the environment variable DOCKER_HOST. If you IP is constantly changing for example because you are working with VMs in the cloud which you create and recreate often, you can try the env variable or just the --host option

docker --host 'ssh://user@host:PORT' container ls

I just realized the option I mentioned will not help you as your problem is not that the IP is changing but that if it is changing you can’t assign an IdentityFile to it. I think I was confusd by that you have a context configured and if the IP changes you would need to regenerate the context too. So you don’t really earn a lot by using a context and it is better to use the host option, but that does not solve your question in the title.

If you don’t have a static hostname either, you could try a wrapper script that regenerates a config file based on the new IP like:

./cloudvm-docker.sh 1.2.3.4 container ls

Add this to your `$HOME/.ssh/config

Include conf.d/cloudvm-dynamic-config

And generate the included file from the wrapper script like

#!/usr/bin/env bash

echo "
Host cloudvm
  Hostname ${1}
  User youruser
  IdentityFile ~/.ssh/cloudvm-id
  StrictHostKeyChecking no
" > ~/.ssh/conf.d/cloudvm-dynamic-config

docker --host "ssh://cloudvm"  "${@:2}

Then before you try the script set the executable permission

chmod +x ./cloudvm-docker.s