Skip to content
This repository was archived by the owner on Mar 22, 2022. It is now read-only.

Commit 08a7a3c

Browse files
committed
Pull image if without when create service
Signed-off-by: skanehira <[email protected]>
1 parent e8730c5 commit 08a7a3c

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

compose-ref.go

+6
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ func doUp(project string, config *compose.Config) error {
201201
if err != nil {
202202
return err
203203
}
204+
204205
return createService(cli, project, prjDir, service, networks)
205206
})
206207

@@ -221,6 +222,11 @@ func doUp(project string, config *compose.Config) error {
221222
func createService(cli *client.Client, project string, prjDir string, s compose.ServiceConfig, networks map[string]string) error {
222223
ctx := context.Background()
223224

225+
err := internal.PullImageIfWithout(cli, ctx, s.Name)
226+
if err != nil {
227+
return err
228+
}
229+
224230
var shmSize int64
225231
if s.ShmSize != "" {
226232
v, err := units.RAMInBytes(s.ShmSize)

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ require (
55
github.com/Microsoft/go-winio v0.4.14 // indirect
66
github.com/compose-spec/compose-go v0.0.0-20200131085702-0b38cc2d8e6b
77
github.com/containerd/containerd v1.3.2 // indirect
8+
github.com/docker/cli v0.0.0-20200303215952-eb310fca4956
89
github.com/docker/distribution v2.7.1+incompatible // indirect
910
github.com/docker/docker v1.4.2-0.20191113042239-ea84732a7725
1011
github.com/docker/go-connections v0.4.0

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:ma
1515
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1616
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1717
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
18+
github.com/docker/cli v0.0.0-20200303215952-eb310fca4956 h1:5/ZRsUbguX7xFNLlbxVQY/yhD3Psy+vylKZrNme5BJs=
19+
github.com/docker/cli v0.0.0-20200303215952-eb310fca4956/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8=
1820
github.com/docker/distribution v2.7.1+incompatible h1:a5mlkVzth6W5A4fOsS3D2EO5BUmsJpcB+cRlLU7cSug=
1921
github.com/docker/distribution v2.7.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
2022
github.com/docker/docker v1.4.2-0.20191113042239-ea84732a7725 h1:9AYKG7Y/KlVWYycokdsCJTxv9WrH2+YrrOxrrSnBiCc=

internal/image.go

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
Copyright 2020 The Compose Specification Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package internal
18+
19+
import (
20+
"context"
21+
"fmt"
22+
"os"
23+
24+
"github.com/docker/cli/cli/streams"
25+
"github.com/docker/docker/api/types"
26+
"github.com/docker/docker/api/types/filters"
27+
"github.com/docker/docker/client"
28+
"github.com/docker/docker/pkg/jsonmessage"
29+
)
30+
31+
func PullImageIfWithout(cli *client.Client, ctx context.Context, name string) error {
32+
args := filters.NewArgs(filters.Arg("reference", name))
33+
images, err := cli.ImageList(ctx, types.ImageListOptions{All: true, Filters: args})
34+
if err != nil {
35+
return err
36+
}
37+
if len(images) == 0 {
38+
fmt.Println("pulling image: " + name)
39+
res, err := cli.ImagePull(ctx, name, types.ImagePullOptions{})
40+
if err != nil {
41+
return err
42+
}
43+
44+
out := streams.NewOut(os.Stdout)
45+
err = jsonmessage.DisplayJSONMessagesToStream(res, out, nil)
46+
if err != nil {
47+
return err
48+
}
49+
}
50+
return nil
51+
}

0 commit comments

Comments
 (0)