From 8f16cb8a712f0fdd0f07ea05a733e65bc6a45116 Mon Sep 17 00:00:00 2001 From: Alex Goodman Date: Sun, 13 May 2018 14:54:17 -0400 Subject: [PATCH] just getting comfy with docker api and images --- Dockerfile | 3 +++ README.md | 5 ++++- main.go | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 Dockerfile create mode 100644 main.go diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..407597d --- /dev/null +++ b/Dockerfile @@ -0,0 +1,3 @@ +FROM alpine:latest +ADD README.md /somefile.txt +RUN cp /somefile.txt /root/somefile.txt \ No newline at end of file diff --git a/README.md b/README.md index b084ece..d3edb03 100644 --- a/README.md +++ b/README.md @@ -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 +``` \ No newline at end of file diff --git a/main.go b/main.go new file mode 100644 index 0000000..7bae80c --- /dev/null +++ b/main.go @@ -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 + } + } +}