Subject: bug#74219: 29.4; TRAMP uses the same ad-hoc proxy for every docker container on remote hosts

From: Michael Albinus <[email-masked]>
Date: Thu, 07 Nov 2024 19:48:35 +0100

Artem Smushkov <[email-masked]> writes:

Hi Artem,

your subject isn't correct. True is "Tramp uses the same ad-hoc proxy
for every docker container with the same name on remote hosts".

> I need to access some number of similarly named docker containers on
> different remote hosts. For example I run
>
> cd "/ssh:host1|docker:container_name:/"
>
> in eshell, run some commands there, then I may run
>
> cd "/ssh:host2|docker:container_name:/"
>
> and run some commands there, etc. Emacs lisp functions (like cd, ls,
> find-file, dired, etc) work as expected, but when I run some
> executable
> there (bash, python, etc) it always connects to the same host no
> matter
> what I specified in "cd" command previously. The workaround to make
> things work is to run
>
> (setq tramp-default-proxies-alist nil)
>
> before switching to different host.

That's not possible with Tramp. Not with the current design. The proxy
definitions are kept internally, and they apply in reverse order of
their appearance in the remote file names.

After you have accessed "/ssh:host1|docker:container_name:", Tramp
caches the information, that the remote host "/docker:container_name:"
is reachable via "/ssh:host1:". You can even use the short form
"/docker:container_name:" now, and Tramp remembers the hops.

When you give a different multi-hop remote file name, like
"/ssh:host2|docker:container_name:", Tramp parses from the end, and it
detects, that it should use the hop "/ssh:host1:" as proxy. No further
check. That's why you see the observed behavior.

One could argue that this is an error. I'm not so sure, it is rather a
consequence of the design. Proxy declarations were introduced in Tramp by
means of configuring tramp-default-proxies-alist. This list is organized
to detect proxy hops by starting with the final, target host, and adding
the proxies to pass in order to reach that target. People found the
configuration of that user option too complex, and that's why ad-hoc
multi-hop remote file names have been added years later. But the
principle has been kept.

One possible mitigation is to set tramp-default-proxies-alist to nil
before using different ad-hoc multi-hop file names. However, this is too
drastic in my mind, because you will lose *all* specifications there,
even for other multi-hops.

Another, and better, solution is to use different target names. Do you
need to give the containers the very same name "container_name" on both
"host1" and "host2"? If you would give them different names, the problem
doesn't exist.

Best regards, Michael.