Merge pull request #989 from docker/build_command

introduce `compose build` command
This commit is contained in:
Nicolas De loof 2020-11-30 13:06:38 +01:00 committed by GitHub
commit e9d20d512a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 108 additions and 0 deletions

View File

@ -44,6 +44,10 @@ func newComposeService(ctx store.AciContext) aciComposeService {
}
}
func (cs *aciComposeService) Build(ctx context.Context, project *types.Project) error {
return errdefs.ErrNotImplemented
}
func (cs *aciComposeService) Up(ctx context.Context, project *types.Project, detach bool) error {
logrus.Debugf("Up on project with name %q", project.Name)

View File

@ -29,6 +29,10 @@ import (
type composeService struct {
}
func (c *composeService) Build(ctx context.Context, project *types.Project) error {
return errdefs.ErrNotImplemented
}
// Up executes the equivalent to a `compose up`
func (c *composeService) Up(context.Context, *types.Project, bool) error {
return errdefs.ErrNotImplemented

View File

@ -25,6 +25,8 @@ import (
// Service manages a compose project
type Service interface {
// Build executes the equivalent to a `compose build`
Build(ctx context.Context, project *types.Project) error
// Up executes the equivalent to a `compose up`
Up(ctx context.Context, project *types.Project, detach bool) error
// Down executes the equivalent to a `compose down`

67
cli/cmd/compose/build.go Normal file
View File

@ -0,0 +1,67 @@
/*
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.
*/
package compose
import (
"context"
"github.com/compose-spec/compose-go/cli"
"github.com/spf13/cobra"
"github.com/docker/compose-cli/api/client"
"github.com/docker/compose-cli/progress"
)
type buildOptions struct {
composeOptions
}
func buildCommand() *cobra.Command {
opts := buildOptions{}
buildCmd := &cobra.Command{
Use: "build [SERVICE...]",
RunE: func(cmd *cobra.Command, args []string) error {
return runBuild(cmd.Context(), opts, args)
},
}
return buildCmd
}
func runBuild(ctx context.Context, opts buildOptions, services []string) error {
c, err := client.New(ctx)
if err != nil {
return err
}
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
options, err := opts.toProjectOptions()
if err != nil {
return "", err
}
project, err := cli.ProjectFromOptions(options)
if err != nil {
return "", err
}
err = filter(project, services)
if err != nil {
return "", err
}
return "", c.ComposeService().Build(ctx, project)
})
return err
}

View File

@ -25,6 +25,7 @@ import (
"github.com/spf13/pflag"
"github.com/docker/compose-cli/api/client"
"github.com/docker/compose-cli/context/store"
"github.com/docker/compose-cli/errdefs"
)
@ -89,6 +90,10 @@ func Command(contextType string) *cobra.Command {
convertCommand(),
)
if contextType == store.LocalContextType {
command.AddCommand(buildCommand())
}
return command
}

View File

@ -41,6 +41,10 @@ import (
"golang.org/x/mod/semver"
)
func (e ecsLocalSimulation) Build(ctx context.Context, project *types.Project) error {
return errdefs.ErrNotImplemented
}
func (e ecsLocalSimulation) Up(ctx context.Context, project *types.Project, detach bool) error {
cmd := exec.Command("docker-compose", "version", "--short")
b := bytes.Buffer{}

View File

@ -24,8 +24,13 @@ import (
"syscall"
"github.com/compose-spec/compose-go/types"
"github.com/docker/compose-cli/errdefs"
)
func (b *ecsAPIService) Build(ctx context.Context, project *types.Project) error {
return errdefs.ErrNotImplemented
}
func (b *ecsAPIService) Up(ctx context.Context, project *types.Project, detach bool) error {
err := b.aws.CheckRequirements(ctx, b.Region)
if err != nil {

View File

@ -138,6 +138,11 @@ func (cs *containerService) Delete(ctx context.Context, id string, request conta
type composeService struct{}
func (cs *composeService) Build(ctx context.Context, project *types.Project) error {
fmt.Printf("Build command on project %q", project.Name)
return nil
}
func (cs *composeService) Up(ctx context.Context, project *types.Project, detach bool) error {
fmt.Printf("Up command on project %q", project.Name)
return nil

View File

@ -29,6 +29,7 @@ import (
"strings"
"github.com/compose-spec/compose-go/types"
"github.com/docker/buildx/build"
moby "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
@ -53,6 +54,17 @@ type composeService struct {
apiClient *client.Client
}
func (s *composeService) Build(ctx context.Context, project *types.Project) error {
opts := map[string]build.Options{}
for _, service := range project.Services {
if service.Build != nil {
opts[service.Name] = s.toBuildOptions(service, project.WorkingDir)
}
}
return s.build(ctx, project, opts)
}
func (s *composeService) Up(ctx context.Context, project *types.Project, detach bool) error {
err := s.ensureImagesExists(ctx, project)
if err != nil {