Merge pull request #9984 from glours/build-image-depends-on

check if a missing image won't be build via a service declared in depends_on section
This commit is contained in:
Guillaume Lours 2022-11-16 13:04:53 +01:00 committed by GitHub
commit ea32fc99e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 58 additions and 1 deletions

View File

@ -271,7 +271,7 @@ func (s *composeService) pullRequiredImages(ctx context.Context, project *types.
eg.Go(func() error { eg.Go(func() error {
id, err := s.pullServiceImage(ctx, service, info, s.configFile(), w, quietPull) id, err := s.pullServiceImage(ctx, service, info, s.configFile(), w, quietPull)
pulledImages[i] = id pulledImages[i] = id
if err != nil && service.Build != nil { if err != nil && isServiceImageToBuild(service, project.Services) {
// image can be built, so we can ignore pull failure // image can be built, so we can ignore pull failure
return nil return nil
} }
@ -291,6 +291,19 @@ func (s *composeService) pullRequiredImages(ctx context.Context, project *types.
}) })
} }
func isServiceImageToBuild(service types.ServiceConfig, services []types.ServiceConfig) bool {
if service.Build != nil {
return true
}
for _, depService := range services {
if depService.Image == service.Image && depService.Build != nil {
return true
}
}
return false
}
func toPullProgressEvent(parent string, jm jsonmessage.JSONMessage, w progress.Writer) { func toPullProgressEvent(parent string, jm jsonmessage.JSONMessage, w progress.Writer) {
if jm.ID == "" || jm.Progress == nil { if jm.ID == "" || jm.Progress == nil {
return return

View File

@ -0,0 +1,16 @@
# Copyright 2020 Docker Compose CLI 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.
FROM busybox:1.35.0
RUN echo "hello"

View File

@ -0,0 +1,9 @@
services:
foo:
image: built-image-dependency
build:
context: .
bar:
image: built-image-dependency
depends_on:
- foo

View File

@ -104,3 +104,22 @@ func TestUpDependenciesNotStopped(t *testing.T) {
RequireServiceState(t, c, "dependency", "running") RequireServiceState(t, c, "dependency", "running")
RequireServiceState(t, c, "orphan", "running") RequireServiceState(t, c, "orphan", "running")
} }
func TestUpWithBuildDependencies(t *testing.T) {
c := NewParallelCLI(t)
t.Run("up with service using image build by an another service", func(t *testing.T) {
// ensure local test run does not reuse previously build image
c.RunDockerOrExitError(t, "rmi", "built-image-dependency")
res := c.RunDockerComposeCmd(t, "--project-directory", "fixtures/dependencies",
"-f", "fixtures/dependencies/service-image-depends-on.yaml", "up", "-d")
t.Cleanup(func() {
c.RunDockerComposeCmd(t, "--project-directory", "fixtures/dependencies",
"-f", "fixtures/dependencies/service-image-depends-on.yaml", "down", "--rmi", "all")
})
res.Assert(t, icmd.Success)
})
}