1

I am trying to access a remote docker daemon using ssh. I managed to do this using docker cli.

docker -H ssh://user@host images

Is there a way to do the same using Golang Docker SDK?

1
  • be aware system intentionally blocks ability to launch containers onto remote host ... all launching and lifecycle actions need to get executed local to host ... hence motive for container orchestration tools to install an agent local to each host ... typically such agent reads from etcd instructions to issue the lifecycle commands ... in turn etcd is written to by some controller doing the actual decision making Commented Oct 2 at 14:14

1 Answer 1

12

After going through Docker CLI source code, I managed to do the above using Golang Docker SDK. Below is a code snippet.

package main

import (
    "context"
    "fmt"
    "github.com/docker/cli/cli/connhelper"
    "github.com/docker/docker/api/types"
    "github.com/docker/docker/client"
    "net/http"
    "os"
)

func main(){

    helper, err := connhelper.GetConnectionHelper("ssh://user@host:22")

    if err != nil{
        return
    }

    httpClient := &http.Client{
        // No tls
        // No proxy
        Transport: &http.Transport{
            DialContext: helper.Dialer,
        },
    }

    var clientOpts []client.Opt

    clientOpts = append(clientOpts,
        client.WithHTTPClient(httpClient),
        client.WithHost(helper.Host),
        client.WithDialContext(helper.Dialer),

    )

    version := os.Getenv("DOCKER_API_VERSION")

    if version != "" {
        clientOpts = append(clientOpts, client.WithVersion(version))
    } else {
        clientOpts = append(clientOpts, client.WithAPIVersionNegotiation())
    }


    cl, err := client.NewClientWithOpts(clientOpts...)


    if err != nil {
        fmt.Println("Unable to create docker client")
        panic(err)
    }

    fmt.Println(cl.ImageList(context.Background(), types.ImageListOptions{}))

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.