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?
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?
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{}))
}