Skip to content

Commit bcaa908

Browse files
PIG208ndeloof
authored andcommitted
Support COMPOSE_IGNORE_ORPHANS for compose run
This revives docker#7020 and resolves the issue mentioned in docker#4992. Signed-off-by: Zixuan James Li <[email protected]>
1 parent c64b044 commit bcaa908

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

cmd/compose/run.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ type runOptions struct {
5353
servicePorts bool
5454
name string
5555
noDeps bool
56+
ignoreOrphans bool
5657
quietPull bool
5758
}
5859

@@ -134,6 +135,8 @@ func runCommand(p *projectOptions, backend api.Service) *cobra.Command {
134135
if err != nil {
135136
return err
136137
}
138+
ignore := project.Environment["COMPOSE_IGNORE_ORPHANS"]
139+
opts.ignoreOrphans = strings.ToLower(ignore) == "true"
137140
return runRun(ctx, backend, project, opts)
138141
}),
139142
ValidArgsFunction: serviceCompletion(p),
@@ -182,7 +185,7 @@ func runRun(ctx context.Context, backend api.Service, project *types.Project, op
182185
}
183186

184187
err = progress.Run(ctx, func(ctx context.Context) error {
185-
return startDependencies(ctx, backend, *project, opts.Service)
188+
return startDependencies(ctx, backend, *project, opts.Service, opts.ignoreOrphans)
186189
})
187190
if err != nil {
188191
return err
@@ -229,7 +232,7 @@ func runRun(ctx context.Context, backend api.Service, project *types.Project, op
229232
return err
230233
}
231234

232-
func startDependencies(ctx context.Context, backend api.Service, project types.Project, requestedServiceName string) error {
235+
func startDependencies(ctx context.Context, backend api.Service, project types.Project, requestedServiceName string, ignoreOrphans bool) error {
233236
dependencies := types.Services{}
234237
var requestedService types.ServiceConfig
235238
for _, service := range project.Services {
@@ -242,7 +245,9 @@ func startDependencies(ctx context.Context, backend api.Service, project types.P
242245

243246
project.Services = dependencies
244247
project.DisabledServices = append(project.DisabledServices, requestedService)
245-
if err := backend.Create(ctx, &project, api.CreateOptions{}); err != nil {
248+
if err := backend.Create(ctx, &project, api.CreateOptions{
249+
IgnoreOrphans: ignoreOrphans,
250+
}); err != nil {
246251
return err
247252
}
248253
return backend.Start(ctx, project.Name, api.StartOptions{})

pkg/e2e/compose_run_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,22 @@ func TestLocalComposeRun(t *testing.T) {
9898
assert.Assert(t, strings.Contains(res.Stdout(), "8081->80/tcp"), res.Stdout())
9999
})
100100

101+
t.Run("compose run orphan", func(t *testing.T) {
102+
// Use different compose files to get an orphan container
103+
c.RunDockerComposeCmd("-f", "./fixtures/run-test/orphan.yaml", "run", "simple")
104+
res := c.RunDockerComposeCmd("-f", "./fixtures/run-test/compose.yaml", "run", "back", "echo", "Hello")
105+
assert.Assert(t, strings.Contains(res.Combined(), "orphan"))
106+
107+
cmd := c.NewDockerCmd("compose", "-f", "./fixtures/run-test/compose.yaml", "run", "back", "echo", "Hello")
108+
res = icmd.RunCmd(cmd, func(cmd *icmd.Cmd) {
109+
cmd.Env = append(cmd.Env, "COMPOSE_IGNORE_ORPHANS=True")
110+
})
111+
assert.Assert(t, !strings.Contains(res.Combined(), "orphan"))
112+
})
113+
101114
t.Run("down", func(t *testing.T) {
102115
c.RunDockerComposeCmd("-f", "./fixtures/run-test/compose.yaml", "down")
116+
c.RunDockerComposeCmd("-f", "./fixtures/run-test/orphan.yaml", "down")
103117
res := c.RunDockerCmd("ps", "--all")
104118
assert.Assert(t, !strings.Contains(res.Stdout(), "run-test"), res.Stdout())
105119
})

pkg/e2e/fixtures/run-test/orphan.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
version: '3.8'
2+
services:
3+
simple:
4+
image: alpine
5+
command: echo "Hi there!!"

0 commit comments

Comments
 (0)