Merge pull request #1044 from gtardif/compose_build_when_needed

No rebuild on compose up if image already exists by default
This commit is contained in:
Nicolas De loof 2020-12-10 18:47:12 +01:00 committed by GitHub
commit 4f883cae26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 41 additions and 20 deletions

View File

@ -35,6 +35,7 @@ type composeOptions struct {
Environment []string
Format string
Detach bool
Build bool
Quiet bool
}

View File

@ -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 {

2
go.mod
View File

@ -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

4
go.sum
View File

@ -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=

View File

@ -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 {

View File

@ -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")