just getting comfy with docker api and images

This commit is contained in:
Alex Goodman 2018-05-13 14:54:17 -04:00
parent f12ac1d63b
commit 8f16cb8a71
No known key found for this signature in database
GPG Key ID: 05328C611D8A520E
3 changed files with 63 additions and 1 deletions

3
Dockerfile Normal file
View File

@ -0,0 +1,3 @@
FROM alpine:latest
ADD README.md /somefile.txt
RUN cp /somefile.txt /root/somefile.txt

View File

@ -1,2 +1,5 @@
# docker-image-explorer
I'll probably rename this since it acronyms to D.I.E. ... details...
```
docker build -t . die-test:latest
go run main.go
```

56
main.go Normal file
View File

@ -0,0 +1,56 @@
package main
import (
"encoding/json"
"fmt"
"github.com/docker/docker/client"
"golang.org/x/net/context"
)
func main() {
ctx := context.Background()
cli, err := client.NewEnvClient()
if err != nil {
panic(err)
}
// imageID := "golang:alpine"
imageID := "die-test:latest"
for {
inspect, _, err := cli.ImageInspectWithRaw(ctx, imageID)
if err != nil {
panic(err)
}
history, err := cli.ImageHistory(ctx, imageID)
if err != nil {
panic(err)
}
historyStr, err := json.MarshalIndent(history, "", " ")
if err != nil {
panic(err)
}
layerStr := ""
for idx, layer := range inspect.RootFS.Layers {
prefix := "├── "
if idx == len(inspect.RootFS.Layers)-1 {
prefix = "└── "
}
layerStr += fmt.Sprintf("%s%s\n", prefix, layer)
}
fmt.Printf("Image: %s\nId: %s\nParent: %s\nLayers: %d\n%sHistory: %s\n", imageID, inspect.ID, inspect.Parent, len(inspect.RootFS.Layers), layerStr, historyStr)
fmt.Println("\n")
if inspect.Parent == "" {
break
} else {
imageID = inspect.Parent
}
}
}