Parcourir la source

Auto-remove using container config

Signed-off-by: Guillaume Tardif <[email protected]>
Guillaume Tardif il y a 4 ans
Parent
commit
bad0d41d90
5 fichiers modifiés avec 21 ajouts et 33 suppressions
  1. 4 3
      api/compose/api.go
  2. 5 11
      cli/cmd/compose/run.go
  3. 6 6
      local/compose/convergence.go
  4. 2 1
      local/compose/create.go
  5. 4 12
      local/compose/run.go

+ 4 - 3
api/compose/api.go

@@ -70,9 +70,10 @@ type ConvertOptions struct {
 
 // RunOptions holds all flags for compose run
 type RunOptions struct {
-	Name    string
-	Command []string
-	Detach  bool
+	Name       string
+	Command    []string
+	Detach     bool
+	AutoRemove bool
 }
 
 // PortPublisher hold status about published port

+ 5 - 11
cli/cmd/compose/run.go

@@ -24,7 +24,6 @@ import (
 	"github.com/spf13/cobra"
 
 	"github.com/docker/compose-cli/api/compose"
-	"github.com/docker/compose-cli/api/containers"
 	"github.com/docker/compose-cli/progress"
 )
 
@@ -73,15 +72,15 @@ func runRun(ctx context.Context, opts runOptions) error {
 		return err
 	}
 
-	dependencies := []types.ServiceConfig{}
 	originalServices := project.Services
-	containerID, err := progress.Run(ctx, func(ctx context.Context) (string, error) {
+	_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
+		dependencies := types.Services{}
 		for _, service := range originalServices {
 			if service.Name != opts.Name {
 				dependencies = append(dependencies, service)
 			}
 		}
-		project.Services = types.Services(dependencies)
+		project.Services = dependencies
 		if err := c.ComposeService().Create(ctx, project); err != nil {
 			return "", err
 		}
@@ -96,18 +95,13 @@ func runRun(ctx context.Context, opts runOptions) error {
 
 	project.Services = originalServices
 	// start container and attach to container streams
-	containerID, err = c.ComposeService().RunOneOffContainer(ctx, project, compose.RunOptions{Name: opts.Name, Command: opts.Command, Detach: opts.Detach})
+	runOpts := compose.RunOptions{Name: opts.Name, Command: opts.Command, Detach: opts.Detach, AutoRemove: opts.Remove}
+	containerID, err := c.ComposeService().RunOneOffContainer(ctx, project, runOpts)
 	if err != nil {
 		return err
 	}
 	if opts.Detach {
 		fmt.Printf("%s", containerID)
-		return nil
-	}
-	if opts.Remove {
-		return c.ContainerService().Delete(ctx, containerID, containers.DeleteRequest{
-			Force: true,
-		})
 	}
 	return nil
 }

+ 6 - 6
local/compose/convergence.go

@@ -62,7 +62,7 @@ func (s *composeService) ensureService(ctx context.Context, project *types.Proje
 			number := next + i
 			name := fmt.Sprintf("%s_%s_%d", project.Name, service.Name, number)
 			eg.Go(func() error {
-				return s.createContainer(ctx, project, service, name, number)
+				return s.createContainer(ctx, project, service, name, number, false)
 			})
 		}
 	}
@@ -163,10 +163,10 @@ func getScale(config types.ServiceConfig) int {
 	return 1
 }
 
-func (s *composeService) createContainer(ctx context.Context, project *types.Project, service types.ServiceConfig, name string, number int) error {
+func (s *composeService) createContainer(ctx context.Context, project *types.Project, service types.ServiceConfig, name string, number int, autoRemove bool) error {
 	w := progress.ContextWriter(ctx)
 	w.Event(progress.CreatingEvent(name))
-	err := s.runContainer(ctx, project, service, name, number, nil)
+	err := s.createMobyContainer(ctx, project, service, name, number, nil, autoRemove)
 	if err != nil {
 		return err
 	}
@@ -191,7 +191,7 @@ func (s *composeService) recreateContainer(ctx context.Context, project *types.P
 	if err != nil {
 		return err
 	}
-	err = s.runContainer(ctx, project, service, name, number, &container)
+	err = s.createMobyContainer(ctx, project, service, name, number, &container, false)
 	if err != nil {
 		return err
 	}
@@ -228,8 +228,8 @@ func (s *composeService) restartContainer(ctx context.Context, container moby.Co
 	return nil
 }
 
-func (s *composeService) runContainer(ctx context.Context, project *types.Project, service types.ServiceConfig, name string, number int, container *moby.Container) error {
-	containerConfig, hostConfig, networkingConfig, err := getContainerCreateOptions(project, service, number, container)
+func (s *composeService) createMobyContainer(ctx context.Context, project *types.Project, service types.ServiceConfig, name string, number int, container *moby.Container, autoRemove bool) error {
+	containerConfig, hostConfig, networkingConfig, err := getContainerCreateOptions(project, service, number, container, autoRemove)
 	if err != nil {
 		return err
 	}

+ 2 - 1
local/compose/create.go

@@ -91,7 +91,7 @@ func (s *composeService) ensureProjectVolumes(ctx context.Context, project *type
 	return nil
 }
 
-func getContainerCreateOptions(p *types.Project, s types.ServiceConfig, number int, inherit *moby.Container) (*container.Config, *container.HostConfig, *network.NetworkingConfig, error) {
+func getContainerCreateOptions(p *types.Project, s types.ServiceConfig, number int, inherit *moby.Container, autoRemove bool) (*container.Config, *container.HostConfig, *network.NetworkingConfig, error) {
 	hash, err := jsonHash(s)
 	if err != nil {
 		return nil, nil, nil, err
@@ -167,6 +167,7 @@ func getContainerCreateOptions(p *types.Project, s types.ServiceConfig, number i
 
 	networkMode := getNetworkMode(p, s)
 	hostConfig := container.HostConfig{
+		AutoRemove:     autoRemove,
 		Mounts:         mountOptions,
 		CapAdd:         strslice.StrSlice(s.CapAdd),
 		CapDrop:        strslice.StrSlice(s.CapDrop),

+ 4 - 12
local/compose/run.go

@@ -23,7 +23,6 @@ import (
 
 	"github.com/compose-spec/compose-go/types"
 	"github.com/docker/compose-cli/api/compose"
-	"github.com/docker/compose-cli/utils"
 	apitypes "github.com/docker/docker/api/types"
 	"github.com/docker/docker/api/types/filters"
 	"golang.org/x/sync/errgroup"
@@ -56,8 +55,7 @@ func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.
 	if err := s.waitDependencies(ctx, project, requestedService); err != nil {
 		return "", err
 	}
-	err := s.createContainer(ctx, project, requestedService, requestedService.ContainerName, 1)
-	if err != nil {
+	if err := s.createContainer(ctx, project, requestedService, requestedService.ContainerName, 1, opts.AutoRemove); err != nil {
 		return "", err
 	}
 
@@ -69,19 +67,14 @@ func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.
 
 	containers, err := s.apiClient.ContainerList(ctx, apitypes.ContainerListOptions{
 		Filters: filters.NewArgs(
-			projectFilter(project.Name),
+			filters.Arg("label", fmt.Sprintf("%s=%s", slugLabel, slug)),
 		),
 		All: true,
 	})
 	if err != nil {
 		return "", err
 	}
-	var oneoffContainer apitypes.Container
-	for _, container := range containers {
-		if utils.StringContains(container.Names, "/"+containerID) {
-			oneoffContainer = container
-		}
-	}
+	oneoffContainer := containers[0]
 	eg := errgroup.Group{}
 	eg.Go(func() error {
 		return s.attachContainerStreams(ctx, oneoffContainer, true, os.Stdin, os.Stdout)
@@ -90,8 +83,7 @@ func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.
 		return "", err
 	}
 
-	err = s.apiClient.ContainerStart(ctx, containerID, apitypes.ContainerStartOptions{})
-	if err != nil {
+	if err = s.apiClient.ContainerStart(ctx, containerID, apitypes.ContainerStartOptions{}); err != nil {
 		return "", err
 	}
 	err = eg.Wait()