Skip to content

Instantly share code, notes, and snippets.

@agbaraka
Created September 14, 2019 08:21
Show Gist options
  • Save agbaraka/654a218f8ea13b3da8a47d47595f5d05 to your computer and use it in GitHub Desktop.
Save agbaraka/654a218f8ea13b3da8a47d47595f5d05 to your computer and use it in GitHub Desktop.
Access remote docker daemon via ssh using Golang Docker SDK
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://[email protected]: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{}))
}
@Roxxers
Copy link

Roxxers commented Jul 16, 2021

👍 ✨

@AhnMo
Copy link

AhnMo commented Nov 23, 2021

👍 * 3000

@nathan815
Copy link

nathan815 commented Apr 24, 2024

Dependencies:

go get github.com/docker/cli/cli
go get github.com/docker/docker/client
go get github.com/docker/docker/api

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment