Skip to content

SSH connection is not being reused when sending multiple commands #2966

@VelorumS

Description

cmds.sh connects through SSH and executes several commands:

#!/usr/bin/env python3

import docker


if __name__ == "__main__":
    client = docker.DockerClient("ssh://localhost", use_ssh_client=True)
    client.ping()
    client.images.list()
    client.containers.list()

Running this script spawns a new ssh connection for each command which takes a lot of time because the whole login sequence is repeated:

$ strace -fs256 ./cmds.py 2>&1 | grep exec.*ssh
[pid 35337] execve("/bin/sh", ["/bin/sh", "-c", "ssh -p 22 -- localhost docker system dial-stdio"], 0x14d3a90 /* 72 vars */ <unfinished ...>
[pid 35338] execve("/usr/bin/ssh", ["ssh", "-p", "22", "--", "localhost", "docker", "system", "dial-stdio"], 0x561272c46448 /* 72 vars */ <unfinished ...>
[pid 35405] execve("/bin/sh", ["/bin/sh", "-c", "ssh -p 22 -- localhost docker system dial-stdio"], 0x14d8090 /* 72 vars */ <unfinished ...>
[pid 35406] execve("/usr/bin/ssh", ["ssh", "-p", "22", "--", "localhost", "docker", "system", "dial-stdio"], 0x55c16b5e1448 /* 72 vars */ <unfinished ...>
[pid 35465] execve("/bin/sh", ["/bin/sh", "-c", "ssh -p 22 -- localhost docker system dial-stdio"], 0x14d8090 /* 72 vars */ <unfinished ...>
[pid 35466] execve("/usr/bin/ssh", ["ssh", "-p", "22", "--", "localhost", "docker", "system", "dial-stdio"], 0x55c40c8e4448 /* 72 vars */ <unfinished ...>
[pid 35524] execve("/bin/sh", ["/bin/sh", "-c", "ssh -p 22 -- localhost docker system dial-stdio"], 0x14d8090 /* 72 vars */ <unfinished ...>
[pid 35525] execve("/usr/bin/ssh", ["ssh", "-p", "22", "--", "localhost", "docker", "system", "dial-stdio"], 0x55ae78291448 /* 72 vars */ <unfinished ...>
[pid 35583] execve("/bin/sh", ["/bin/sh", "-c", "ssh -p 22 -- localhost docker system dial-stdio"], 0x14dcfd0 /* 72 vars */ <unfinished ...>
[pid 35584] execve("/usr/bin/ssh", ["ssh", "-p", "22", "--", "localhost", "docker", "system", "dial-stdio"], 0x55b6878d6448 /* 72 vars */ <unfinished ...>
[pid 35642] execve("/bin/sh", ["/bin/sh", "-c", "ssh -p 22 -- localhost docker system dial-stdio"], 0x14dabc0 /* 72 vars */ <unfinished ...>
[pid 35643] execve("/usr/bin/ssh", ["ssh", "-p", "22", "--", "localhost", "docker", "system", "dial-stdio"], 0x562746674448 /* 72 vars */ <unfinished ...>
[pid 35702] execve("/bin/sh", ["/bin/sh", "-c", "ssh -p 22 -- localhost docker system dial-stdio"], 0x14d8090 /* 72 vars */ <unfinished ...>
[pid 35703] execve("/usr/bin/ssh", ["ssh", "-p", "22", "--", "localhost", "docker", "system", "dial-stdio"], 0x555dd2d9a448 /* 72 vars */ <unfinished ...>

I expected it to reuse the same pipe for all commands.

Even using the paramiko client (use_ssh_client=False) seems to be similar because SSHConnectionPool are created for every URL. Those URLs are different for every request.

So I don't really understand how this was supposed to work efficiently. Is there something fundamental that precludes the reuse of the SSH connection?

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions