Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backup: If container image name doesn't specify the tag, find the correct/used one. #23

Merged
merged 1 commit into from
May 2, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,40 @@ func backupTar(filename string, backup Backup) error {
return nil
}

func getFullImageName(imageName string) (string, error) {
// If the image already specifies a tag we can safely use as-is
if strings.Contains(imageName, ":") {
return imageName, nil
}

// If the used image doesn't include tag information try to find one (if it exists).
images, err := cli.ImageList(ctx, types.ImageListOptions{})
if err != nil {
// Couldn't get image list, abort
return imageName, err
}

for _, image := range images {
if (!strings.Contains(imageName, image.ID)) || len(image.RepoTags) == 0 {
// unrelated image or image entry doesn't have any tags, move on
continue
}

for _, tag := range image.RepoTags {
// use closer matching tag if it exists
if !strings.Contains(tag, imageName) {
continue
}
return tag, nil
}
// If none of the tags matches the base image name, return the first tag
return image.RepoTags[0], nil
}

// There is no tag on the matching image, just have to go with what was provided
muesli marked this conversation as resolved.
Show resolved Hide resolved
return imageName, nil
}

func backup(ID string) error {
conf, err := cli.ContainerInspect(ctx, ID)
if err != nil {
Expand All @@ -155,6 +189,12 @@ func backup(ID string) error {
fmt.Printf("Creating backup of %s (%s, %s)\n", conf.Name[1:], conf.Config.Image, conf.ID[:12])

paths = []string{}

conf.Config.Image, err = getFullImageName(conf.Config.Image)
if err != nil {
return err
}

backup := Backup{
PortMap: conf.HostConfig.PortBindings,
Config: conf.Config,
Expand Down