Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: respect dependency order for classic builder #9587

Merged
merged 2 commits into from
Jun 24, 2022
Merged
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
2 changes: 1 addition & 1 deletion pkg/compose/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func (s *composeService) doBuild(ctx context.Context, project *types.Project, op
return nil, nil
}
if buildkitEnabled, err := s.dockerCli.BuildKitEnabled(); err != nil || !buildkitEnabled {
return s.doBuildClassic(ctx, opts)
return s.doBuildClassic(ctx, project, opts)
}
return s.doBuildBuildkit(ctx, project, opts, mode)
}
Expand Down
16 changes: 13 additions & 3 deletions pkg/compose/build_classic.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"runtime"
"strings"

"github.com/compose-spec/compose-go/types"
buildx "github.com/docker/buildx/build"
"github.com/docker/cli/cli/command/image/build"
dockertypes "github.com/docker/docker/api/types"
Expand All @@ -41,15 +42,24 @@ import (
"github.com/pkg/errors"
)

func (s *composeService) doBuildClassic(ctx context.Context, opts map[string]buildx.Options) (map[string]string, error) {
func (s *composeService) doBuildClassic(ctx context.Context, project *types.Project, opts map[string]buildx.Options) (map[string]string, error) {
var nameDigests = make(map[string]string)
var errs error
for name, o := range opts {
err := project.WithServices(nil, func(service types.ServiceConfig) error {
imageName := getImageName(service, project.Name)
o, ok := opts[imageName]
if !ok {
return nil
}
digest, err := s.doBuildClassicSimpleImage(ctx, o)
if err != nil {
errs = multierror.Append(errs, err).ErrorOrNil()
}
nameDigests[name] = digest
nameDigests[imageName] = digest
return nil
})
if err != nil {
return nil, err
}

return nameDigests, errs
Expand Down
37 changes: 37 additions & 0 deletions pkg/e2e/compose_build_test.go → pkg/e2e/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,40 @@ func TestBuildTags(t *testing.T) {
res.Assert(t, icmd.Expected{Out: expectedOutput})
})
}

func TestBuildImageDependencies(t *testing.T) {
doTest := func(t *testing.T, cli *CLI) {
resetState := func() {
cli.RunDockerComposeCmd(t, "down", "--rmi=all", "-t=0")
}
resetState()
t.Cleanup(resetState)

// the image should NOT exist now
res := cli.RunDockerOrExitError(t, "image", "inspect", "build-dependencies_service")
res.Assert(t, icmd.Expected{
ExitCode: 1,
Err: "Error: No such image: build-dependencies_service",
})

res = cli.RunDockerComposeCmd(t, "build")
t.Log(res.Combined())

res = cli.RunDockerCmd(t,
"image", "inspect", "--format={{ index .RepoTags 0 }}",
"build-dependencies_service")
res.Assert(t, icmd.Expected{Out: "build-dependencies_service:latest"})
}

t.Run("ClassicBuilder", func(t *testing.T) {
cli := NewParallelCLI(t, WithEnv(
"DOCKER_BUILDKIT=0",
"COMPOSE_FILE=./fixtures/build-dependencies/compose.yaml",
))
doTest(t, cli)
})

t.Run("BuildKit", func(t *testing.T) {
t.Skip("See https://github.com/docker/compose/issues/9232")
})
}
19 changes: 19 additions & 0 deletions pkg/e2e/fixtures/build-dependencies/base.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 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 alpine

COPY hello.txt /hello.txt

CMD [ "/bin/true" ]
12 changes: 12 additions & 0 deletions pkg/e2e/fixtures/build-dependencies/compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
services:
base:
image: base
build:
context: .
dockerfile: base.dockerfile
service:
depends_on:
- base
build:
context: .
dockerfile: service.dockerfile
1 change: 1 addition & 0 deletions pkg/e2e/fixtures/build-dependencies/hello.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this file was copied from base -> service
19 changes: 19 additions & 0 deletions pkg/e2e/fixtures/build-dependencies/service.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 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 alpine

COPY --from=base /hello.txt /hello.txt

CMD [ "cat", "/hello.txt" ]