MCPcopy
hub / github.com/spacecloud-io/space-cloud / PullImageIfNotExist

Function PullImageIfNotExist

space-cli/cmd/utils/docker.go:77–97  ·  view source on GitHub ↗

PullImageIfNotExist pulls the docker image if it does not exist

(ctx context.Context, dockerClient *client.Client, image string)

Source from the content-addressed store, hash-verified

75
76// PullImageIfNotExist pulls the docker image if it does not exist
77func PullImageIfNotExist(ctx context.Context, dockerClient *client.Client, image string) error {
78 _, _, err := dockerClient.ImageInspectWithRaw(ctx, image)
79 if err != nil {
80 // pull image from public repository
81 logrus.Infof("Image %s does not exist. Need to pull it from Docker Hub. This may take some time.", image)
82 out, err := dockerClient.ImagePull(ctx, image, types.ImagePullOptions{})
83 if err != nil {
84 logrus.Errorf("Unable to pull public image with id (%s) - %s", image, err.Error())
85 return err
86 }
87 s := spinner.New(spinner.CharSets[11], 100*time.Millisecond) // Build our new spinner
88 s.Suffix = " Downloading image..."
89 _ = s.Color("green")
90 s.Start()
91 time.Sleep(4 * time.Second) // Run for some time to simulate work// Start the spinner
92 _, _ = io.Copy(ioutil.Discard, out)
93 s.Stop()
94 }
95 logrus.Infof("Image %s already exists. No need to pull it again", image)
96 return nil
97}
98
99// DockerfileGolang is the docker file to use for golang projects
100const DockerfileGolang string = `

Callers

nothing calls this directly

Calls 2

ErrorMethod · 0.65
StartMethod · 0.45

Tested by

no test coverage detected