有一个直接的方式来获得来自unix套接字的html响应(像curl一样)吗?

我需要创build一个Docker(1.13)容器,它将作为docker swarm中的一个服务运行,以安排作业(比如在需要时执行“docker exec”的全局crontab)。 我是一个相当系统pipe理员,而不是一个编码器,所以我开始用bash,curl和jq来做这件事。 它有效,但是肯定有改进的余地。

为了给你一个我正在处理的mumbo-jumpo的概念,下面是我传递给docker套接字以查明服务在哪里运行的一些片段:

# Get local docker node ID: curl -s --unix-socket /var/run/docker.sock http:/v1.26/info | jq -r '.Name + " " + .Swarm.NodeID' # List swarm node ID's: curl -s --unix-socket /var/run/docker.sock http:/v1.26/nodes | jq -r '.[] | .Description.Hostname + " " + .ID' # List swarm services: curl -s --unix-socket /var/run/docker.sock http:/v1.26/nodes | jq -r '.[] | .Description.Hostname + " " + .ID' # List running tasks: curl -s --unix-socket /var/run/docker.sock http:/v1.26/tasks | jq -r '.[] | select( .Status.State | contains("running")) | .NodeID + " " + .ServiceID + " " + .ID + " " + .Status.State' 

我想用golang做得更好

我知道http.Get如何与TCP套接字交谈时工作:

 res, err := http.Get(url) body, err := ioutil.ReadAll(res.Body) err = json.Unmarshal(body, &p) // p is then populated with the content of my request 

但是我怎样才能处理一个Unix文件夹(/var/run/docker.sock)? 我假设我必须使用net.DialUnix,但不能得到它的工作到目前为止。

任何想法或build议是受欢迎的。

尝试这个:

 package main import ( "fmt" "net" "log" "bufio" ) func main() { conn, err := net.Dial("unix", "/var/run/docker.sock") if err != nil { panic(err) } fmt.Fprintf(conn, "GET /images/json HTTP/1.0\r\n\r\n") status, err := bufio.NewReader(conn).ReadString('\t') if err != nil { log.Println(err) } fmt.Print("Message from server: "+status) } 

另一种方法:

使用docker SDK ,可用于python,去和其他一些语言。