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

Commit

Permalink
Pull image if without when create service
Browse files Browse the repository at this point in the history
Signed-off-by: skanehira <[email protected]>
  • Loading branch information
skanehira committed May 6, 2020
1 parent e8730c5 commit 77dde60
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
6 changes: 6 additions & 0 deletions compose-ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ func doUp(project string, config *compose.Config) error {
if err != nil {
return err
}

return createService(cli, project, prjDir, service, networks)
})

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

err := internal.PullImageIfWithout(cli, ctx, s.Name)
if err != nil {
return err
}

var shmSize int64
if s.ShmSize != "" {
v, err := units.RAMInBytes(s.ShmSize)
Expand Down
45 changes: 45 additions & 0 deletions internal/image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Copyright 2020 The Compose Specification Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package internal

import (
"context"
"fmt"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
)

func PullImageIfWithout(cli *client.Client, ctx context.Context, name string) error {
args := filters.NewArgs(filters.Arg("reference", name))
images, err := cli.ImageList(ctx, types.ImageListOptions{All: true, Filters: args})
if err != nil {
return err
}
if len(images) == 0 {
fmt.Println("pulling image: " + name)
_, err := cli.ImagePull(ctx, name, types.ImagePullOptions{})
if err != nil {
return err
}

// TODO use github.com/docker/cli/cli/streams to dispaly stream message
}

return nil
}

0 comments on commit 77dde60

Please sign in to comment.