-
-
Save nathan815/45e15b4f82c1c40ad9d4224a79a0f8c7 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{})) | |
} |
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
module mymodule | |
go 1.21.5 | |
require ( | |
github.com/docker/cli v26.1.0+incompatible | |
github.com/docker/docker v26.1.0+incompatible | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment