Go Program to Read Docker Image List From Unix Socket (/var/run/docker.sock)
It took me a bit of time to get this simple program working so I'm sharing for other people new to Go.
package main
import (
"fmt"
"io"
"net"
)
func reader(r io.Reader) {
buf := make([]byte, 1024)
for {
n, err := r.Read(buf[:])
if err != nil {
return
}
println(string(buf[0:n]))
}
}
func main() {
c, err := net.Dial("unix", "/var/run/docker.sock")
if err != nil {
panic(err)
}
defer c.Close()
fmt.Fprintf(c, "GET /images/json HTTP/1.0\r\n\r\n")
reader(c)
}
package main
import (
"fmt"
"io"
"net"
)
func reader(r io.Reader) {
buf := make([]byte, 1024)
for {
n, err := r.Read(buf[:])
if err != nil {
return
}
println(string(buf[0:n]))
}
}
func main() {
c, err := net.Dial("unix", "/var/run/docker.sock")
if err != nil {
panic(err)
}
defer c.Close()
fmt.Fprintf(c, "GET /images/json HTTP/1.0\r\n\r\n")
reader(c)
}