Auto-remove using container config

Signed-off-by: Guillaume Tardif <guillaume.tardif@gmail.com>
This commit is contained in:
Guillaume Tardif 2020-12-16 18:34:14 +01:00
parent d2cfffafb4
commit bad0d41d90
5 changed files with 21 additions and 33 deletions

View File

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

View File

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

View File

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

View File

@ -91,7 +91,7 @@ func (s *composeService) ensureProjectVolumes(ctx context.Context, project *type
return nil 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) hash, err := jsonHash(s)
if err != nil { if err != nil {
return nil, nil, nil, err return nil, nil, nil, err
@ -167,6 +167,7 @@ func getContainerCreateOptions(p *types.Project, s types.ServiceConfig, number i
networkMode := getNetworkMode(p, s) networkMode := getNetworkMode(p, s)
hostConfig := container.HostConfig{ hostConfig := container.HostConfig{
AutoRemove: autoRemove,
Mounts: mountOptions, Mounts: mountOptions,
CapAdd: strslice.StrSlice(s.CapAdd), CapAdd: strslice.StrSlice(s.CapAdd),
CapDrop: strslice.StrSlice(s.CapDrop), CapDrop: strslice.StrSlice(s.CapDrop),

View File

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