dockerpy sdk unable to connect to remote server

430 views Asked by At

I'm currently trying to use the dockerpy sdk to connect to my remote ubuntu server so i can manage my docker containers via python.

I am getting a few issues when attempting to do this.

docker.APIClient(base_url="ssh://user@ip")

When doing the following I am getting the error:

paramiko.ssh_exception.PasswordRequiredException: private key file is encrypted

I can resolve this issue by adding the kwarg: use_ssh_client, but then i am forced to input a password, which limits the potential for automation.

docker.APIClient(base_url="ssh://user:@ip", use_ssh_client=True)

When using the above code, I have also tried to enter my ssh key password into the base_url such as:

docker.APIClient(base_url="ssh://user:pass@ip", use_ssh_client=True)

However, this then greets me with the following error:

docker.errors.DockerException: Invalid bind address format: ssh://root:pass@ip

I have run out of ideas and am confused as to how I am supposed to get around this?

Many thanks in advance...

2

There are 2 answers

2
mrAndrew On

It's possible to make a connection as Mr. Piere answered here. Even with that question about docker.client.DockerClient which uses docker.api.client.APIClient under the hood.

You are trying to establish a connection using Password Authentication that's why you asked to prompt a password. I guess you need to configure the Key-Based SSH Login as said in docker's docs

Steps to fix:

  1. configure SSH Login on a remote server and fill ~/.ssh/config on your local machine

  2. connect from the local terminal using the ssh command to ensure a connection is established without asking password ssh user@ip

  3. connect using library client = docker. APIClient(base_url="ssh://user@ip", use_ssh_client=True)

0
DevMax On

I had a similar Problem. Your Problem is, that you Key is encrypted. The Docker Clients doesn't have a Passphrase option by default. I wrote some Code based on this Post. It works for me :)

import os
from docker import APIClient
from docker.transport import SSHHTTPAdapter

class MySSHHTTPAdapter(SSHHTTPAdapter):
    def _connect(self):
        if self.ssh_client:
            self.ssh_params["key_filename"] = os.environ.get("SSH_KEY_FILENAME")
            self.ssh_params["passphrase"] = os.environ.get("SSH_PASSPHRASE")
            self.ssh_client.connect(**self.ssh_params)


client = APIClient('ssh://ip:22', use_ssh_client=True, version='1.41')
ssh_adapter = MySSHHTTPAdapter('ssh://user@ip:22')
client.mount('http+docker://ssh', ssh_adapter)
print(client.version())