docker api通过ssh方式调用远程主机的接口,前提是通过免密登陆
免密登陆可参考:https://mp.csdn.net/mp_blog/creation/editor/126679013
- import(
- "net/http"
-
- "github.com/docker/cli/cli/connhelper"
- "github.com/docker/docker/client"
- )
-
- func getClientBySsh() (cli *client.Client, err error) {
- //user为远程主机的用户名,一般是root,ip是远程主机的具体ip地址
- helper, _ := connhelper.GetConnectionHelper("ssh://user@ip:22")
- httpClient := &http.Client{
- Transport: &http.Transport{
- DialContext: helper.Dialer,
- },
- }
-
- cli, err = client.NewClientWithOpts(
- client.WithHTTPClient(httpClient),
- client.WithHost(helper.Host),
- client.WithDialContext(helper.Dialer),
- client.WithVersion("1.40"), //指定当前docker部署的版本
- )
-
- return
- }
相关操作可参考官网:https://docs.docker.com/engine/api/sdk/examples/
官方通过client.FromEnv方式初始化,适用于本级docker client初始化,不适合远程调用