Skip to content

Commit

Permalink
Pin single docker base image from commandline argument
Browse files Browse the repository at this point in the history
Fetch the latest hash for a single docker base image supplied as
commandline argument.

Example:
```
$ dockpin docker-base ubuntu:16.04
ubuntu@sha256:0f71fa8d4d2d4292c3c617fda2b36f6dabe5c8b6e34c3dc5b0d17d4e704bd39c
```
  • Loading branch information
oh6hay authored and Jille committed Feb 24, 2022
1 parent e33c0d3 commit 6807c85
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
24 changes: 24 additions & 0 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ var (
RunE: runDockerPin,
}

dockerResolveCmd = &cobra.Command{
Use: "resolve [base-image]",
Example: "resolve ubuntu:20.04",
Short: "Prints the current digest of the given base image",
Args: cobra.ExactArgs(1),
SilenceUsage: true,
RunE: runDockerResolve,
}

dockerfile *string
)

Expand Down Expand Up @@ -57,6 +66,21 @@ func runDockerPin(cmd *cobra.Command, args []string) error {
return ioutil.WriteFile(ifDash(*dockerfile, "/dev/stdout"), n, 0644)
}

func runDockerResolve(cmd *cobra.Command, args []string) error {
baseImageAndVersion := args[0]
dockerClient, err := docker.NewClientWithOpts(docker.FromEnv)
if err != nil {
return err
}
di, err := dockerClient.DistributionInspect(cmd.Context(), baseImageAndVersion, "")
if err != nil {
return err
}
hashedBase := baseImageAndVersion + "@" + string(di.Descriptor.Digest)
_, err = fmt.Println(hashedBase)
return err
}

func ifDash(fn string, repl string) string {
if fn == "-" {
return repl
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func main() {
aptCmd.AddCommand(aptPinCmd)
aptCmd.AddCommand(aptInstallCmd)
dockerCmd.AddCommand(dockerPinCmd)
dockerCmd.AddCommand(dockerResolveCmd)
rootCmd.AddCommand(aptCmd)
rootCmd.AddCommand(dockerCmd)
if err := rootCmd.Execute(); err != nil {
Expand Down

0 comments on commit 6807c85

Please sign in to comment.