diff --git a/cmd/compose/compose.go b/cmd/compose/compose.go index 75aef36be..42416ff70 100644 --- a/cmd/compose/compose.go +++ b/cmd/compose/compose.go @@ -342,11 +342,14 @@ func (o *ProjectOptions) ToProject(ctx context.Context, dockerCli command.Cli, s project.Services[name] = s } + project, err = project.WithSelectedServices(services) + if err != nil { + return nil, tracing.Metrics{}, err + } + if !o.All { project = project.WithoutUnnecessaryResources() } - - project, err = project.WithSelectedServices(services) return project, metrics, err } diff --git a/pkg/compose/create.go b/pkg/compose/create.go index ccf920580..b48df9d03 100644 --- a/pkg/compose/create.go +++ b/pkg/compose/create.go @@ -1307,11 +1307,10 @@ func (s *composeService) resolveExternalNetwork(ctx context.Context, n *types.Ne if len(networks) == 0 { // in this instance, n.Name is really an ID sn, err := s.apiClient().NetworkInspect(ctx, n.Name, network.InspectOptions{}) - if err != nil { + if err != nil && !errdefs.IsNotFound(err) { return err } networks = append(networks, sn) - } // NetworkList API doesn't return the exact name match, so we can retrieve more than one network with a request diff --git a/pkg/e2e/compose_test.go b/pkg/e2e/compose_test.go index ce5752f25..c84fd9ed6 100644 --- a/pkg/e2e/compose_test.go +++ b/pkg/e2e/compose_test.go @@ -388,3 +388,20 @@ func TestNestedDotEnv(t *testing.T) { }) } + +func TestUnnecesaryResources(t *testing.T) { + const projectName = "compose-e2e-unnecessary-resources" + c := NewParallelCLI(t) + t.Cleanup(func() { + c.RunDockerComposeCmd(t, "-p", projectName, "down", "-t=0") + }) + + res := c.RunDockerComposeCmdNoCheck(t, "-f", "./fixtures/external/compose.yaml", "-p", projectName, "up", "-d") + res.Assert(t, icmd.Expected{ + ExitCode: 1, + Err: "network foo_bar declared as external, but could not be found", + }) + + c.RunDockerComposeCmd(t, "-f", "./fixtures/external/compose.yaml", "-p", projectName, "up", "-d", "test") + // Should not fail as missing external network is not used +} diff --git a/pkg/e2e/fixtures/external/compose.yaml b/pkg/e2e/fixtures/external/compose.yaml new file mode 100644 index 000000000..29b8b74a1 --- /dev/null +++ b/pkg/e2e/fixtures/external/compose.yaml @@ -0,0 +1,14 @@ +services: + test: + image: nginx:alpine + + other: + image: nginx:alpine + networks: + test_network: + ipv4_address: 8.8.8.8 + +networks: + test_network: + external: true + name: foo_bar \ No newline at end of file