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

Commit 77dde60

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

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-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)

internal/image.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
23+
"github.com/docker/docker/api/types"
24+
"github.com/docker/docker/api/types/filters"
25+
"github.com/docker/docker/client"
26+
)
27+
28+
func PullImageIfWithout(cli *client.Client, ctx context.Context, name string) error {
29+
args := filters.NewArgs(filters.Arg("reference", name))
30+
images, err := cli.ImageList(ctx, types.ImageListOptions{All: true, Filters: args})
31+
if err != nil {
32+
return err
33+
}
34+
if len(images) == 0 {
35+
fmt.Println("pulling image: " + name)
36+
_, err := cli.ImagePull(ctx, name, types.ImagePullOptions{})
37+
if err != nil {
38+
return err
39+
}
40+
41+
// TODO use github.com/docker/cli/cli/streams to dispaly stream message
42+
}
43+
44+
return nil
45+
}

0 commit comments

Comments
 (0)