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

Pull image if without when create service #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
44 changes: 44 additions & 0 deletions internal/image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
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 print json message stream
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

incomplete PR?

}
return nil
}