From cf378834e56515b95df6e85ced0157ccd7634710 Mon Sep 17 00:00:00 2001 From: Guillaume Tardif Date: Thu, 10 Dec 2020 10:22:18 +0100 Subject: [PATCH] No rebuild on compose up if image already exists by default, added first use of compose pull_policy and `compose up --build` to allow to force rebuild. Signed-off-by: Guillaume Tardif --- cli/cmd/compose/compose.go | 1 + cli/cmd/compose/up.go | 8 ++++++- go.mod | 2 +- go.sum | 4 ++-- local/compose/build.go | 37 ++++++++++++++++++------------- tests/compose-e2e/compose_test.go | 9 ++++++++ 6 files changed, 41 insertions(+), 20 deletions(-) diff --git a/cli/cmd/compose/compose.go b/cli/cmd/compose/compose.go index 37cc21c83..2881060cf 100644 --- a/cli/cmd/compose/compose.go +++ b/cli/cmd/compose/compose.go @@ -35,6 +35,7 @@ type composeOptions struct { Environment []string Format string Detach bool + Build bool Quiet bool } diff --git a/cli/cmd/compose/up.go b/cli/cmd/compose/up.go index 854825b0e..0be7eb9b1 100644 --- a/cli/cmd/compose/up.go +++ b/cli/cmd/compose/up.go @@ -51,7 +51,8 @@ func upCommand(contextType string) *cobra.Command { upCmd.Flags().StringVar(&opts.WorkingDir, "workdir", "", "Work dir") upCmd.Flags().StringArrayVarP(&opts.ConfigPaths, "file", "f", []string{}, "Compose configuration files") upCmd.Flags().StringArrayVarP(&opts.Environment, "environment", "e", []string{}, "Environment variables") - upCmd.Flags().BoolVarP(&opts.Detach, "detach", "d", false, " Detached mode: Run containers in the background") + upCmd.Flags().BoolVarP(&opts.Detach, "detach", "d", false, "Detached mode: Run containers in the background") + upCmd.Flags().BoolVar(&opts.Build, "build", false, "Build images before starting containers.") if contextType == store.AciContextType { upCmd.Flags().StringVar(&opts.DomainName, "domainname", "", "Container NIS domain name") @@ -119,6 +120,11 @@ func setup(ctx context.Context, opts composeOptions, services []string) (*client // arbitrarily set the domain name on the first service ; ACI backend will expose the entire project project.Services[0].DomainName = opts.DomainName } + if opts.Build { + for _, service := range project.Services { + service.PullPolicy = types.PullPolicyBuild + } + } err = filter(project, services) if err != nil { diff --git a/go.mod b/go.mod index 98870777c..3b3631771 100644 --- a/go.mod +++ b/go.mod @@ -32,7 +32,7 @@ require ( github.com/aws/aws-sdk-go v1.35.33 github.com/awslabs/goformation/v4 v4.15.6 github.com/buger/goterm v0.0.0-20200322175922-2f3e71b85129 - github.com/compose-spec/compose-go v0.0.0-20201208135325-bcfd1e07a97e + github.com/compose-spec/compose-go v0.0.0-20201210155915-b5ef325e9175 github.com/containerd/console v1.0.1 github.com/containerd/containerd v1.4.0-0 github.com/containerd/continuity v0.0.0-20200928162600-f2cc35102c2a // indirect diff --git a/go.sum b/go.sum index 5452c6740..4a61f227d 100644 --- a/go.sum +++ b/go.sum @@ -156,8 +156,8 @@ github.com/cloudflare/cfssl v0.0.0-20181213083726-b94e044bb51e h1:Qux+lbuMaRzkQy github.com/cloudflare/cfssl v0.0.0-20181213083726-b94e044bb51e/go.mod h1:yMWuSON2oQp+43nFtAV/uvKQIFpSPerB57DCt9t8sSA= github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= github.com/codahale/hdrhistogram v0.0.0-20160425231609-f8ad88b59a58/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI= -github.com/compose-spec/compose-go v0.0.0-20201208135325-bcfd1e07a97e h1:sfRp4eLv+F9ml4Yh49DPkIlEj07N0p/56FzCLbbROJ0= -github.com/compose-spec/compose-go v0.0.0-20201208135325-bcfd1e07a97e/go.mod h1:rz7rjxJGA/pWpLdBmDdqymGm2okEDYgBE7yx569xW+I= +github.com/compose-spec/compose-go v0.0.0-20201210155915-b5ef325e9175 h1:6ZE967wCKnx4h+OIUsjnS113itBlncF3ls/Ia7rKcbc= +github.com/compose-spec/compose-go v0.0.0-20201210155915-b5ef325e9175/go.mod h1:rz7rjxJGA/pWpLdBmDdqymGm2okEDYgBE7yx569xW+I= github.com/containerd/cgroups v0.0.0-20190919134610-bf292b21730f/go.mod h1:OApqhQ4XNSNC13gXIwDjhOQxjWa/NxkwZXJ1EvqT0ko= github.com/containerd/cgroups v0.0.0-20200217135630-d732e370d46d/go.mod h1:CStdkl05lBnJej94BPFoJ7vB8cELKXwViS+dgfW0/M8= github.com/containerd/cgroups v0.0.0-20200531161412-0dbf7f05ba59 h1:qWj4qVYZ95vLWwqyNJCQg7rDsG5wPdze0UaPolH7DUk= diff --git a/local/compose/build.go b/local/compose/build.go index 041372393..ea799299b 100644 --- a/local/compose/build.go +++ b/local/compose/build.go @@ -35,10 +35,7 @@ func (s *composeService) Build(ctx context.Context, project *types.Project) erro opts := map[string]build.Options{} for _, service := range project.Services { if service.Build != nil { - imageName := service.Image - if imageName == "" { - imageName = project.Name + "_" + service.Name - } + imageName := getImageName(service, project) opts[imageName] = s.toBuildOptions(service, project.WorkingDir, imageName) } } @@ -46,6 +43,14 @@ func (s *composeService) Build(ctx context.Context, project *types.Project) erro return s.build(ctx, project, opts) } +func getImageName(service types.ServiceConfig, project *types.Project) string { + imageName := service.Image + if imageName == "" { + imageName = project.Name + "_" + service.Name + } + return imageName +} + func (s *composeService) ensureImagesExists(ctx context.Context, project *types.Project) error { opts := map[string]build.Options{} for _, service := range project.Services { @@ -53,20 +58,20 @@ func (s *composeService) ensureImagesExists(ctx context.Context, project *types. return fmt.Errorf("invalid service %q. Must specify either image or build", service.Name) } + imageName := getImageName(service, project) + localImagePresent, err := s.localImagePresent(ctx, imageName) + if err != nil { + return err + } // TODO build vs pull should be controlled by pull policy, see https://github.com/compose-spec/compose-spec/issues/26 if service.Image != "" { - needPull, err := s.needPull(ctx, service) - if err != nil { - return err - } - if !needPull { + if localImagePresent { continue } } if service.Build != nil { - imageName := service.Image - if imageName == "" { - imageName = project.Name + "_" + service.Name + if localImagePresent && service.PullPolicy != types.PullPolicyBuild { + continue } opts[imageName] = s.toBuildOptions(service, project.WorkingDir, imageName) continue @@ -89,15 +94,15 @@ func (s *composeService) ensureImagesExists(ctx context.Context, project *types. return s.build(ctx, project, opts) } -func (s *composeService) needPull(ctx context.Context, service types.ServiceConfig) (bool, error) { - _, _, err := s.apiClient.ImageInspectWithRaw(ctx, service.Image) +func (s *composeService) localImagePresent(ctx context.Context, imageName string) (bool, error) { + _, _, err := s.apiClient.ImageInspectWithRaw(ctx, imageName) if err != nil { if errdefs.IsNotFound(err) { - return true, nil + return false, nil } return false, err } - return false, nil + return true, nil } func (s *composeService) build(ctx context.Context, project *types.Project, opts map[string]build.Options) error { diff --git a/tests/compose-e2e/compose_test.go b/tests/compose-e2e/compose_test.go index 72ce59d52..6328a14d4 100644 --- a/tests/compose-e2e/compose_test.go +++ b/tests/compose-e2e/compose_test.go @@ -123,6 +123,9 @@ func TestLocalComposeBuild(t *testing.T) { c.RunDockerOrExitError("rmi", "custom-nginx") res := c.RunDockerCmd("compose", "up", "-d", "--workdir", "fixtures/build-test") + t.Cleanup(func() { + c.RunDockerCmd("compose", "down", "--workdir", "fixtures/build-test") + }) res.Assert(t, icmd.Expected{Out: "COPY static /usr/share/nginx/html"}) @@ -133,6 +136,12 @@ func TestLocalComposeBuild(t *testing.T) { c.RunDockerCmd("image", "inspect", "custom-nginx") }) + t.Run("no rebuild when up again", func(t *testing.T) { + res := c.RunDockerCmd("compose", "up", "-d", "--workdir", "fixtures/build-test") + + assert.Assert(t, !strings.Contains(res.Stdout(), "COPY static /usr/share/nginx/html"), res.Stdout()) + }) + t.Run("cleanup build project", func(t *testing.T) { c.RunDockerCmd("compose", "down", "--workdir", "fixtures/build-test") c.RunDockerCmd("rmi", "build-test_nginx")