Created
September 14, 2019 08:21
-
-
Save agbaraka/654a218f8ea13b3da8a47d47595f5d05 to your computer and use it in GitHub Desktop.
Access remote docker daemon via ssh using Golang Docker SDK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{})) | |
} |
👍 * 3000
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
👍 ✨