mirror of
https://github.com/docker/compose.git
synced 2025-07-26 07:04:32 +02:00
Merge pull request #9173 from KoditkarVedant/8768-avoid-pulling-same-image-multiple-times
Avoid pulling same images multiple times ⚡️
This commit is contained in:
commit
f880b4129c
@ -21,6 +21,7 @@ import (
|
|||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -64,13 +65,16 @@ func (s *composeService) pull(ctx context.Context, project *types.Project, opts
|
|||||||
eg, ctx := errgroup.WithContext(ctx)
|
eg, ctx := errgroup.WithContext(ctx)
|
||||||
|
|
||||||
var mustBuild []string
|
var mustBuild []string
|
||||||
|
|
||||||
|
imagesBeingPulled := map[string]string{}
|
||||||
|
|
||||||
for _, service := range project.Services {
|
for _, service := range project.Services {
|
||||||
service := service
|
service := service
|
||||||
if service.Image == "" {
|
if service.Image == "" {
|
||||||
w.Event(progress.Event{
|
w.Event(progress.Event{
|
||||||
ID: service.Name,
|
ID: service.Name,
|
||||||
Status: progress.Done,
|
Status: progress.Done,
|
||||||
Text: "Skipped",
|
Text: "Skipped - No image to be pulled",
|
||||||
})
|
})
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -88,12 +92,32 @@ func (s *composeService) pull(ctx context.Context, project *types.Project, opts
|
|||||||
w.Event(progress.Event{
|
w.Event(progress.Event{
|
||||||
ID: service.Name,
|
ID: service.Name,
|
||||||
Status: progress.Done,
|
Status: progress.Done,
|
||||||
Text: "Exists",
|
Text: "Skipped - Image is already present locally",
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
if _, ok := images[service.Image]; ok {
|
||||||
|
w.Event(progress.Event{
|
||||||
|
ID: service.Name,
|
||||||
|
Status: progress.Done,
|
||||||
|
Text: "Skipped - Image is already present locally",
|
||||||
})
|
})
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if s, ok := imagesBeingPulled[service.Image]; ok {
|
||||||
|
w.Event(progress.Event{
|
||||||
|
ID: service.Name,
|
||||||
|
Status: progress.Done,
|
||||||
|
Text: fmt.Sprintf("Skipped - Image is already being pulled by %v", s),
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
imagesBeingPulled[service.Image] = service.Name
|
||||||
|
|
||||||
eg.Go(func() error {
|
eg.Go(func() error {
|
||||||
_, err := s.pullServiceImage(ctx, service, info, s.configFile(), w, false)
|
_, err := s.pullServiceImage(ctx, service, info, s.configFile(), w, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -122,11 +122,49 @@ func TestLocalComposeUp(t *testing.T) {
|
|||||||
func TestComposePull(t *testing.T) {
|
func TestComposePull(t *testing.T) {
|
||||||
c := NewParallelCLI(t)
|
c := NewParallelCLI(t)
|
||||||
|
|
||||||
res := c.RunDockerComposeCmd(t, "--project-directory", "fixtures/simple-composefile", "pull")
|
t.Run("Verify image pulled", func(t *testing.T) {
|
||||||
output := res.Combined()
|
// cleanup existing images
|
||||||
|
c.RunDockerComposeCmd(t, "--project-directory", "fixtures/compose-pull/simple", "down", "--rmi", "all")
|
||||||
|
|
||||||
assert.Assert(t, strings.Contains(output, "simple Pulled"))
|
res := c.RunDockerComposeCmd(t, "--project-directory", "fixtures/compose-pull/simple", "pull")
|
||||||
assert.Assert(t, strings.Contains(output, "another Pulled"))
|
output := res.Combined()
|
||||||
|
|
||||||
|
assert.Assert(t, strings.Contains(output, "simple Pulled"))
|
||||||
|
assert.Assert(t, strings.Contains(output, "another Pulled"))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Verify a image is pulled once", func(t *testing.T) {
|
||||||
|
// cleanup existing images
|
||||||
|
c.RunDockerComposeCmd(t, "--project-directory", "fixtures/compose-pull/duplicate-images", "down", "--rmi", "all")
|
||||||
|
|
||||||
|
res := c.RunDockerComposeCmd(t, "--project-directory", "fixtures/compose-pull/duplicate-images", "pull")
|
||||||
|
output := res.Combined()
|
||||||
|
|
||||||
|
if strings.Contains(output, "another Pulled") {
|
||||||
|
assert.Assert(t, strings.Contains(output, "another Pulled"))
|
||||||
|
assert.Assert(t, strings.Contains(output, "Skipped - Image is already being pulled by another"))
|
||||||
|
} else {
|
||||||
|
assert.Assert(t, strings.Contains(output, "simple Pulled"))
|
||||||
|
assert.Assert(t, strings.Contains(output, "Skipped - Image is already being pulled by simple"))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Verify skipped pull if image is already present locally", func(t *testing.T) {
|
||||||
|
// make sure the required image is present
|
||||||
|
c.RunDockerComposeCmd(t, "--project-directory", "fixtures/compose-pull/image-present-locally", "pull")
|
||||||
|
|
||||||
|
res := c.RunDockerComposeCmd(t, "--project-directory", "fixtures/compose-pull/image-present-locally", "pull")
|
||||||
|
output := res.Combined()
|
||||||
|
|
||||||
|
assert.Assert(t, strings.Contains(output, "Skipped - Image is already present locally"))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Verify skipped no image to be pulled", func(t *testing.T) {
|
||||||
|
res := c.RunDockerComposeCmd(t, "--project-directory", "fixtures/compose-pull/no-image-name-given", "pull")
|
||||||
|
output := res.Combined()
|
||||||
|
|
||||||
|
assert.Assert(t, strings.Contains(output, "Skipped - No image to be pulled"))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDownComposefileInParentFolder(t *testing.T) {
|
func TestDownComposefileInParentFolder(t *testing.T) {
|
||||||
|
@ -0,0 +1,7 @@
|
|||||||
|
services:
|
||||||
|
simple:
|
||||||
|
image: alpine:3.13
|
||||||
|
command: top
|
||||||
|
another:
|
||||||
|
image: alpine:3.13
|
||||||
|
command: top
|
@ -0,0 +1,4 @@
|
|||||||
|
services:
|
||||||
|
simple:
|
||||||
|
image: alpine:3.13.12
|
||||||
|
command: top
|
@ -0,0 +1,3 @@
|
|||||||
|
services:
|
||||||
|
no-image-service:
|
||||||
|
build: .
|
7
pkg/e2e/fixtures/compose-pull/simple/docker-compose.yaml
Normal file
7
pkg/e2e/fixtures/compose-pull/simple/docker-compose.yaml
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
services:
|
||||||
|
simple:
|
||||||
|
image: alpine:3.14
|
||||||
|
command: top
|
||||||
|
another:
|
||||||
|
image: alpine:3.15
|
||||||
|
command: top
|
Loading…
x
Reference in New Issue
Block a user