golang遠(yuǎn)程操作docker api方式
golang遠(yuǎn)程操作docker api
Docker 可以監(jiān)聽并處理 3 種 socket 形式的 API 請求,分別是unix(unix 域協(xié)議)、tcp(tcp 協(xié)議)和fd。
一般來說,在安裝好 docker 后,默認(rèn)就已經(jīng)開啟了unix socket,并且我們在執(zhí)行需要有root權(quán)限或者docker用戶組成員才有權(quán)限訪問。例如:
curl --unix-socket /var/run/docker.sock http://docker/version
添加遠(yuǎn)程 API 訪問接口
編輯 docker 守護(hù)進(jìn)程的配置文件/lib/systemd/system/docker.service,找到運(yùn)行主命令的那一行,其內(nèi)容大致為"ExecStart=/usr/bin/dockerd -H fd:// … "的那一行,給dockerd命令加參數(shù)-H tcp://0.0.0.0:2375,意思是在 2375 端口開放 API 訪問。
例如在我的設(shè)備上,配置文件相應(yīng)的那一行原本為
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
添加參數(shù)后變?yōu)?/p>
ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:2375 --containerd=/run/containerd/containerd.sock
重新加載
systemctl daemon-reload # 重新加載守護(hù)進(jìn)程配置 systemctl restart docker.service # 重啟 docker 服務(wù)
測試
comp@Linux system % docker -H tcp://192.168.64.7:2375 version Client: Cloud integration: v1.0.22 Version: 20.10.11 API version: 1.41 Go version: go1.16.10 Git commit: dea9396 Built: Thu Nov 18 00:36:09 2021 OS/Arch: darwin/arm64 Context: default Experimental: true
測試可以訪問
comp@Linux system % docker -H tcp://192.168.64.7:2375 images REPOSITORY TAG IMAGE ID CREATED SIZE gossh v01 fa111aaaec47 8 days ago 35.6MB <none> <none> 670ae487e585 8 days ago 35.6MB nginx latest eeb9db34b331 8 days ago 134MB httpd latest 1c2ff9e4eb7d 2 weeks ago 136MB arm64v8/alpine latest 8e1d7573f448 6 weeks ago 5.33MB alpine latest 8e1d7573f448 6 weeks ago 5.33MB
go 獲取啟動的容器
package main
import (
"context"
"fmt"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
)
func main() {
ctx := context.Background()
//cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation(),
client.WithHost("tcp://192.168.64.7:2375"))
if err != nil {
panic(err)
}
containers, err := cli.ContainerList(ctx, types.ContainerListOptions{})
if err != nil {
panic(err)
}
for _, container := range containers {
fmt.Println(container.Names, container.Image, container.Ports, container.Status,
container.ImageID)
}
}

啟動一個容器
package main
import (
"context"
"fmt"
"io"
"os"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
)
func main() {
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation(),
client.WithHost("tcp://192.168.64.7:2375"))
//cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
panic(err)
}
imageName := "httpd:latest"
out, err := cli.ImagePull(ctx, imageName, types.ImagePullOptions{})
if err != nil {
panic(err)
}
defer out.Close()
io.Copy(os.Stdout, out)
resp, err := cli.ContainerCreate(ctx, &container.Config{
Image: imageName,
}, nil, nil, nil, "")
if err != nil {
panic(err)
}
if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
panic(err)
}
fmt.Println(resp.ID)
}

拉取鏡像
package main
import (
"context"
"io"
"os"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
)
func main() {
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation(),
client.WithHost("tcp://192.168.64.7:2375"))
//cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
panic(err)
}
out, err := cli.ImagePull(ctx, "httpd:latest", types.ImagePullOptions{})
if err != nil {
panic(err)
}
defer out.Close()
io.Copy(os.Stdout, out)
}

官網(wǎng)參考
https://docs.docker.com/engine/api/sdk/examples/
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
golang 刪除切片的某個元素及剔除切片內(nèi)的零值方式
這篇文章主要介紹了golang 刪除切片的某個元素及剔除切片內(nèi)的零值方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04
Go語言循環(huán)遍歷含有中文的字符串的方法小結(jié)
這篇文章主要介紹了Go語言循環(huán)遍歷含有中文的字符串的幾種方法,文章通過代碼示例講解的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴跟著小編一起來看看吧2023-07-07
Go語言中的Panic和Recover,從原理到實(shí)踐
本文詳細(xì)介紹了Go語言中的Panic和Recover機(jī)制,包括它們的基本概念、用法、最佳實(shí)踐以及實(shí)戰(zhàn)應(yīng)用,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2026-04-04
Go表達(dá)式引擎expr基礎(chǔ)用法實(shí)戰(zhàn)指南
expr是一個高性能的 Go 表達(dá)式引擎,它允許你在代碼中安全地執(zhí)行動態(tài)生成的表達(dá)式,本文給大家介紹Go表達(dá)式引擎expr基礎(chǔ)用法實(shí)戰(zhàn)指南,感興趣的朋友跟隨小編一起看看吧2025-10-10
golang快速實(shí)現(xiàn)網(wǎng)頁截圖的方法
這篇文章主要介紹了golang快速實(shí)現(xiàn)網(wǎng)頁截圖的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03

