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
}
originalServices := project.Services
_, 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 {
return err
}
project.Services = originalServices
// start container and attach to container streams
runOpts := compose.RunOptions{
Service: opts.Service,
@ -90,21 +88,24 @@ func runRun(ctx context.Context, opts runOptions) error {
return c.ComposeService().RunOneOffContainer(ctx, project, runOpts)
}
func startDependencies(ctx context.Context, c *client.Client, project *types.Project, requestedService string) error {
originalServices := project.Services
func startDependencies(ctx context.Context, c *client.Client, project types.Project, requestedServiceName string) error {
dependencies := types.Services{}
for _, service := range originalServices {
if service.Name != requestedService {
var requestedService types.ServiceConfig
for _, service := range project.Services {
if service.Name != requestedServiceName {
dependencies = append(dependencies, service)
} else {
requestedService = service
}
}
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
}
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 nil
}

View File

@ -122,6 +122,12 @@ func TestLocalComposeRun(t *testing.T) {
res := c.RunDockerCmd("compose", "-f", "./fixtures/run-test/compose.yml", "run", "back")
lines := Lines(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) {
@ -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")
lines := Lines(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())
})