@@ -17,8 +17,8 @@ import (
17
17
18
18
// Image initializes the given image, and attempts to pull the container from docker hub.
19
19
// If the Build() Option is provided then the given DockerFile tarball is built and returned.
20
- func (c * Client ) Image (ctx context.Context , image string , options ... ImageOption ) (_image * Image , err error ) {
21
- _image = & Image {
20
+ func (c * Client ) Image (ctx context.Context , image string , options ... ImageOption ) (* Image , error ) {
21
+ _image : = & Image {
22
22
client : c ,
23
23
image : image ,
24
24
}
@@ -30,9 +30,10 @@ func (c *Client) Image(ctx context.Context, image string, options ...ImageOption
30
30
}
31
31
}
32
32
33
+ imageExists := _image .checkImageExists (ctx )
33
34
if _image .buildTarball != nil {
34
- if ForceRebuild == true || _image . checkImage ( ctx ) != nil {
35
- err = _image .buildImage (ctx )
35
+ if ForceRebuild == true || ! imageExists {
36
+ err : = _image .buildImage (ctx )
36
37
if err != nil {
37
38
return nil , fmt .Errorf ("building image `%s` failed with %s" , image , err )
38
39
}
@@ -41,28 +42,24 @@ func (c *Client) Image(ctx context.Context, image string, options ...ImageOption
41
42
return _image , nil
42
43
}
43
44
44
- _image , err = _image .Pull (ctx )
45
- if err != nil {
45
+ _image , err : = _image .Pull (ctx )
46
+ if err != nil && ! imageExists {
46
47
return nil , fmt .Errorf ("pulling image `%s` failed with %s" , image , err )
47
48
}
48
49
49
- return _image , err
50
+ return _image , nil
50
51
}
51
52
52
53
// checkImage checks the docker host client if the image is known.
53
- func (i * Image ) checkImage (ctx context.Context ) error {
54
+ func (i * Image ) checkImageExists (ctx context.Context ) bool {
54
55
res , err := i .client .ImageList (ctx , types.ImageListOptions {
55
56
Filters : NewFilter ("reference" , i .image ),
56
57
})
57
- if err != nil {
58
- return fmt . Errorf ( "listing docker host images with reference `%s` failed with %s" , i . image , err )
58
+ if err != nil || len ( res ) < 1 {
59
+ return false
59
60
}
60
61
61
- if len (res ) < 1 {
62
- return fmt .Errorf ("image `%s` not located in docker host" , i .image )
63
- }
64
-
65
- return nil
62
+ return true
66
63
}
67
64
68
65
// buildImage builds a DockerFile tarball as a docker image.
@@ -93,7 +90,7 @@ func (i *Image) buildImage(ctx context.Context) error {
93
90
func (i * Image ) Pull (ctx context.Context ) (* Image , error ) {
94
91
reader , err := i .client .ImagePull (ctx , i .image , types.ImagePullOptions {})
95
92
if err != nil {
96
- return nil , fmt .Errorf ("pulling image `%s`, failed with %s" , i .image , err )
93
+ return i , fmt .Errorf ("pulling image `%s`, failed with %s" , i .image , err )
97
94
}
98
95
99
96
defer reader .Close ()
@@ -113,7 +110,7 @@ func (i *Image) Pull(ctx context.Context) (*Image, error) {
113
110
}
114
111
err = json .Unmarshal (line , & status )
115
112
if err != nil {
116
- return nil , fmt .Errorf ("unmarshaling status from docker pull on image `%s` failed with: %s" , i .image , err )
113
+ return i , fmt .Errorf ("unmarshaling status from docker pull on image `%s` failed with: %s" , i .image , err )
117
114
}
118
115
}
119
116
0 commit comments