mirror of https://github.com/docker/compose.git
Support COMPOSE_IGNORE_ORPHANS for compose run
This revives #7020 and resolves the issue mentioned in #4992. Signed-off-by: Zixuan James Li <359101898@qq.com>
This commit is contained in:
parent
c64b044b7e
commit
bcaa908f74
|
@ -53,6 +53,7 @@ type runOptions struct {
|
||||||
servicePorts bool
|
servicePorts bool
|
||||||
name string
|
name string
|
||||||
noDeps bool
|
noDeps bool
|
||||||
|
ignoreOrphans bool
|
||||||
quietPull bool
|
quietPull bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -134,6 +135,8 @@ func runCommand(p *projectOptions, backend api.Service) *cobra.Command {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
ignore := project.Environment["COMPOSE_IGNORE_ORPHANS"]
|
||||||
|
opts.ignoreOrphans = strings.ToLower(ignore) == "true"
|
||||||
return runRun(ctx, backend, project, opts)
|
return runRun(ctx, backend, project, opts)
|
||||||
}),
|
}),
|
||||||
ValidArgsFunction: serviceCompletion(p),
|
ValidArgsFunction: serviceCompletion(p),
|
||||||
|
@ -182,7 +185,7 @@ func runRun(ctx context.Context, backend api.Service, project *types.Project, op
|
||||||
}
|
}
|
||||||
|
|
||||||
err = progress.Run(ctx, func(ctx context.Context) error {
|
err = progress.Run(ctx, func(ctx context.Context) error {
|
||||||
return startDependencies(ctx, backend, *project, opts.Service)
|
return startDependencies(ctx, backend, *project, opts.Service, opts.ignoreOrphans)
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -229,7 +232,7 @@ func runRun(ctx context.Context, backend api.Service, project *types.Project, op
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func startDependencies(ctx context.Context, backend api.Service, project types.Project, requestedServiceName string) error {
|
func startDependencies(ctx context.Context, backend api.Service, project types.Project, requestedServiceName string, ignoreOrphans bool) error {
|
||||||
dependencies := types.Services{}
|
dependencies := types.Services{}
|
||||||
var requestedService types.ServiceConfig
|
var requestedService types.ServiceConfig
|
||||||
for _, service := range project.Services {
|
for _, service := range project.Services {
|
||||||
|
@ -242,7 +245,9 @@ func startDependencies(ctx context.Context, backend api.Service, project types.P
|
||||||
|
|
||||||
project.Services = dependencies
|
project.Services = dependencies
|
||||||
project.DisabledServices = append(project.DisabledServices, requestedService)
|
project.DisabledServices = append(project.DisabledServices, requestedService)
|
||||||
if err := backend.Create(ctx, &project, api.CreateOptions{}); err != nil {
|
if err := backend.Create(ctx, &project, api.CreateOptions{
|
||||||
|
IgnoreOrphans: ignoreOrphans,
|
||||||
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return backend.Start(ctx, project.Name, api.StartOptions{})
|
return backend.Start(ctx, project.Name, api.StartOptions{})
|
||||||
|
|
|
@ -98,8 +98,22 @@ func TestLocalComposeRun(t *testing.T) {
|
||||||
assert.Assert(t, strings.Contains(res.Stdout(), "8081->80/tcp"), res.Stdout())
|
assert.Assert(t, strings.Contains(res.Stdout(), "8081->80/tcp"), res.Stdout())
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("compose run orphan", func(t *testing.T) {
|
||||||
|
// Use different compose files to get an orphan container
|
||||||
|
c.RunDockerComposeCmd("-f", "./fixtures/run-test/orphan.yaml", "run", "simple")
|
||||||
|
res := c.RunDockerComposeCmd("-f", "./fixtures/run-test/compose.yaml", "run", "back", "echo", "Hello")
|
||||||
|
assert.Assert(t, strings.Contains(res.Combined(), "orphan"))
|
||||||
|
|
||||||
|
cmd := c.NewDockerCmd("compose", "-f", "./fixtures/run-test/compose.yaml", "run", "back", "echo", "Hello")
|
||||||
|
res = icmd.RunCmd(cmd, func(cmd *icmd.Cmd) {
|
||||||
|
cmd.Env = append(cmd.Env, "COMPOSE_IGNORE_ORPHANS=True")
|
||||||
|
})
|
||||||
|
assert.Assert(t, !strings.Contains(res.Combined(), "orphan"))
|
||||||
|
})
|
||||||
|
|
||||||
t.Run("down", func(t *testing.T) {
|
t.Run("down", func(t *testing.T) {
|
||||||
c.RunDockerComposeCmd("-f", "./fixtures/run-test/compose.yaml", "down")
|
c.RunDockerComposeCmd("-f", "./fixtures/run-test/compose.yaml", "down")
|
||||||
|
c.RunDockerComposeCmd("-f", "./fixtures/run-test/orphan.yaml", "down")
|
||||||
res := c.RunDockerCmd("ps", "--all")
|
res := c.RunDockerCmd("ps", "--all")
|
||||||
assert.Assert(t, !strings.Contains(res.Stdout(), "run-test"), res.Stdout())
|
assert.Assert(t, !strings.Contains(res.Stdout(), "run-test"), res.Stdout())
|
||||||
})
|
})
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
version: '3.8'
|
||||||
|
services:
|
||||||
|
simple:
|
||||||
|
image: alpine
|
||||||
|
command: echo "Hi there!!"
|
Loading…
Reference in New Issue