Fix orphans warning when `docker compose run`

Signed-off-by: Guillaume Tardif <guillaume.tardif@gmail.com>
This commit is contained in:
Guillaume Tardif 2021-02-09 17:44:34 +01:00
parent c8b708a20b
commit bf26d056e5
2 changed files with 18 additions and 13 deletions

View File

@ -69,15 +69,13 @@ func runRun(ctx context.Context, opts runOptions) error {
return err return err
} }
originalServices := project.Services
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) { _, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
return "", startDependencies(ctx, c, project, opts.Service) return "", startDependencies(ctx, c, *project, opts.Service)
}) })
if err != nil { if err != nil {
return err return err
} }
project.Services = originalServices
// start container and attach to container streams // start container and attach to container streams
runOpts := compose.RunOptions{ runOpts := compose.RunOptions{
Service: opts.Service, Service: opts.Service,
@ -90,21 +88,24 @@ func runRun(ctx context.Context, opts runOptions) error {
return c.ComposeService().RunOneOffContainer(ctx, project, runOpts) return c.ComposeService().RunOneOffContainer(ctx, project, runOpts)
} }
func startDependencies(ctx context.Context, c *client.Client, project *types.Project, requestedService string) error { func startDependencies(ctx context.Context, c *client.Client, project types.Project, requestedServiceName string) error {
originalServices := project.Services
dependencies := types.Services{} dependencies := types.Services{}
for _, service := range originalServices { var requestedService types.ServiceConfig
if service.Name != requestedService { for _, service := range project.Services {
if service.Name != requestedServiceName {
dependencies = append(dependencies, service) dependencies = append(dependencies, service)
} else {
requestedService = service
} }
} }
project.Services = dependencies project.Services = dependencies
if err := c.ComposeService().Create(ctx, project, compose.CreateOptions{}); err != nil { project.DisabledServices = append(project.DisabledServices, requestedService)
if err := c.ComposeService().Create(ctx, &project, compose.CreateOptions{}); err != nil {
return err return err
} }
if err := c.ComposeService().Start(ctx, project, compose.StartOptions{}); err != nil { if err := c.ComposeService().Start(ctx, &project, compose.StartOptions{}); err != nil {
return err return err
} }
return nil return nil
} }

View File

@ -122,6 +122,12 @@ func TestLocalComposeRun(t *testing.T) {
res := c.RunDockerCmd("compose", "-f", "./fixtures/run-test/compose.yml", "run", "back") res := c.RunDockerCmd("compose", "-f", "./fixtures/run-test/compose.yml", "run", "back")
lines := Lines(res.Stdout()) lines := Lines(res.Stdout())
assert.Equal(t, lines[len(lines)-1], "Hello there!!", res.Stdout()) assert.Equal(t, lines[len(lines)-1], "Hello there!!", res.Stdout())
assert.Assert(t, !strings.Contains(res.Combined(), "orphan"))
res = c.RunDockerCmd("compose", "-f", "./fixtures/run-test/compose.yml", "run", "back", "echo", "Hello one more time")
lines = Lines(res.Stdout())
assert.Equal(t, lines[len(lines)-1], "Hello one more time", res.Stdout())
assert.Assert(t, !strings.Contains(res.Combined(), "orphan"))
}) })
t.Run("check run container exited", func(t *testing.T) { t.Run("check run container exited", func(t *testing.T) {
@ -156,10 +162,8 @@ func TestLocalComposeRun(t *testing.T) {
res := c.RunDockerCmd("compose", "-f", "./fixtures/run-test/compose.yml", "run", "--rm", "back", "/bin/sh", "-c", "echo Hello again") res := c.RunDockerCmd("compose", "-f", "./fixtures/run-test/compose.yml", "run", "--rm", "back", "/bin/sh", "-c", "echo Hello again")
lines := Lines(res.Stdout()) lines := Lines(res.Stdout())
assert.Equal(t, lines[len(lines)-1], "Hello again", res.Stdout()) assert.Equal(t, lines[len(lines)-1], "Hello again", res.Stdout())
})
t.Run("check run container removed", func(t *testing.T) { res = c.RunDockerCmd("ps", "--all")
res := c.RunDockerCmd("ps", "--all")
assert.Assert(t, strings.Contains(res.Stdout(), "run-test_back"), res.Stdout()) assert.Assert(t, strings.Contains(res.Stdout(), "run-test_back"), res.Stdout())
}) })