|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + _ "embed" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/alexflint/go-arg" |
| 13 | +) |
| 14 | + |
| 15 | +//go:embed internal-inspector |
| 16 | +var internalInspector []byte |
| 17 | + |
| 18 | +type Args struct { |
| 19 | + Image string `arg:"positional,required" help:"docker image to inspect"` |
| 20 | + Path string `arg:"--path" default:"/" help:"path inside the container to inspect"` |
| 21 | + JSON bool `arg:"--json" help:"output in JSON format"` |
| 22 | + Summary bool `arg:"--summary" help:"show summary statistics"` |
| 23 | + Pattern string `arg:"--glob" help:"glob pattern for matching files (supports **/)"` |
| 24 | + MD5 bool `arg:"--md5" help:"calculate MD5 checksums for files"` |
| 25 | + Keep bool `arg:"--keep" help:"keep the temporary container after inspection"` |
| 26 | + NoTimes bool `arg:"--no-times" help:"exclude modification times from output"` |
| 27 | +} |
| 28 | + |
| 29 | +func (Args) Version() string { |
| 30 | + return "docker-inspector 1.0.0" |
| 31 | +} |
| 32 | + |
| 33 | +func (Args) Description() string { |
| 34 | + return "Docker image content inspector - examines files and directories inside a container image" |
| 35 | +} |
| 36 | + |
| 37 | +func runInspector(containerID string, args Args) error { |
| 38 | + // Create a temporary directory for the inspector |
| 39 | + tempDir, err := os.MkdirTemp("", "docker-inspector-*") |
| 40 | + if err != nil { |
| 41 | + return fmt.Errorf("failed to create temp dir: %v", err) |
| 42 | + } |
| 43 | + defer os.RemoveAll(tempDir) |
| 44 | + |
| 45 | + // Write the embedded Linux inspector to the temp directory |
| 46 | + inspectorPath := filepath.Join(tempDir, "internal-inspector") |
| 47 | + if err := os.WriteFile(inspectorPath, internalInspector, 0755); err != nil { |
| 48 | + return fmt.Errorf("failed to write inspector: %v", err) |
| 49 | + } |
| 50 | + |
| 51 | + // Copy the inspector to the container |
| 52 | + copyCmd := exec.Command("docker", "cp", inspectorPath, fmt.Sprintf("%s:/inspect", containerID)) |
| 53 | + if output, err := copyCmd.CombinedOutput(); err != nil { |
| 54 | + return fmt.Errorf("failed to copy inspector to container: %v\n%s", err, output) |
| 55 | + } |
| 56 | + |
| 57 | + // Build the command arguments |
| 58 | + dockerArgs := []string{"exec", containerID, "/inspect"} |
| 59 | + if args.JSON { |
| 60 | + dockerArgs = append(dockerArgs, "--json") |
| 61 | + } |
| 62 | + if args.Summary { |
| 63 | + dockerArgs = append(dockerArgs, "--summary") |
| 64 | + } |
| 65 | + if args.Pattern != "" { |
| 66 | + dockerArgs = append(dockerArgs, "--glob", args.Pattern) |
| 67 | + } |
| 68 | + if args.MD5 { |
| 69 | + dockerArgs = append(dockerArgs, "--md5") |
| 70 | + } |
| 71 | + if args.NoTimes { |
| 72 | + dockerArgs = append(dockerArgs, "--no-times") |
| 73 | + } |
| 74 | + if args.Path != "/" { |
| 75 | + dockerArgs = append(dockerArgs, "--path", args.Path) |
| 76 | + } |
| 77 | + |
| 78 | + cmd := exec.Command("docker", dockerArgs...) |
| 79 | + cmd.Stdout = os.Stdout |
| 80 | + cmd.Stderr = os.Stderr |
| 81 | + return cmd.Run() |
| 82 | +} |
| 83 | + |
| 84 | +func main() { |
| 85 | + var args Args |
| 86 | + // Set defaults |
| 87 | + args.Summary = false |
| 88 | + args.Path = "/" |
| 89 | + |
| 90 | + arg.MustParse(&args) |
| 91 | + |
| 92 | + // First, ensure the image exists or can be pulled |
| 93 | + pullCmd := exec.Command("docker", "pull", args.Image) |
| 94 | + pullCmd.Stderr = os.Stderr |
| 95 | + if err := pullCmd.Run(); err != nil { |
| 96 | + fmt.Fprintf(os.Stderr, "Failed to pull image %s: %v\n", args.Image, err) |
| 97 | + os.Exit(1) |
| 98 | + } |
| 99 | + |
| 100 | + // Start a temporary container |
| 101 | + startCmd := exec.Command("docker", "run", "-d", "--entrypoint", "sleep", args.Image, "3600") |
| 102 | + output, err := startCmd.CombinedOutput() |
| 103 | + if err != nil { |
| 104 | + fmt.Fprintf(os.Stderr, "Failed to start container: %v\n%s\n", err, output) |
| 105 | + os.Exit(1) |
| 106 | + } |
| 107 | + |
| 108 | + containerID := strings.TrimSpace(string(output)) |
| 109 | + if containerID == "" { |
| 110 | + fmt.Fprintf(os.Stderr, "Failed to get container ID\n") |
| 111 | + os.Exit(1) |
| 112 | + } |
| 113 | + |
| 114 | + // Give the container a moment to start |
| 115 | + time.Sleep(1 * time.Second) |
| 116 | + |
| 117 | + // Ensure container cleanup unless --keep is specified |
| 118 | + if !args.Keep { |
| 119 | + defer func() { |
| 120 | + stopCmd := exec.Command("docker", "rm", "-f", containerID) |
| 121 | + stopCmd.Run() |
| 122 | + }() |
| 123 | + } |
| 124 | + |
| 125 | + // Run the inspection |
| 126 | + if err := runInspector(containerID, args); err != nil { |
| 127 | + fmt.Fprintf(os.Stderr, "Inspection failed: %v\n", err) |
| 128 | + os.Exit(1) |
| 129 | + } |
| 130 | + |
| 131 | + if args.Keep { |
| 132 | + fmt.Printf("\nContainer ID: %s\n", containerID) |
| 133 | + } |
| 134 | +} |
0 commit comments