Skip to content

Commit d07c437

Browse files
committed
dectect if piped run command and disable tty if so
Signed-off-by: Guillaume Lours <[email protected]>
1 parent da72230 commit d07c437

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

cmd/compose/run.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,11 @@ func runCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *
183183
} else {
184184
options.noTty = !ttyFlag
185185
}
186+
} else if !cmd.Flags().Changed("no-TTY") && !cmd.Flags().Changed("interactive") && !dockerCli.In().IsTerminal() {
187+
// Check if the command was piped or not, if so, force noTty to tru
188+
options.noTty = true
186189
}
190+
187191
if options.quiet {
188192
progress.Mode = progress.ModeQuiet
189193
devnull, err := os.Open(os.DevNull)

pkg/e2e/compose_run_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,60 @@ func TestLocalComposeRun(t *testing.T) {
222222
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/compose.yaml", "run", "build", "echo", "hello world")
223223
res.Assert(t, icmd.Expected{Out: "hello world"})
224224
})
225+
226+
t.Run("compose run with piped input detection", func(t *testing.T) {
227+
if composeStandaloneMode {
228+
t.Skip("Skipping test compose with piped input detection in standalone mode")
229+
}
230+
// Test that piped input is properly detected and TTY is automatically disabled
231+
// This tests the logic added in run.go that checks dockerCli.In().IsTerminal()
232+
cmd := c.NewCmd("sh", "-c", "echo 'piped-content' | docker compose -f ./fixtures/run-test/piped-test.yaml run --rm piped-test")
233+
res := icmd.RunCmd(cmd)
234+
235+
res.Assert(t, icmd.Expected{Out: "piped-content"})
236+
res.Assert(t, icmd.Success)
237+
})
238+
239+
t.Run("compose run piped input should not allocate TTY", func(t *testing.T) {
240+
if composeStandaloneMode {
241+
t.Skip("Skipping test compose with piped input detection in standalone mode")
242+
}
243+
// Test that when stdin is piped, the container correctly detects no TTY
244+
// This verifies that the automatic noTty=true setting works correctly
245+
cmd := c.NewCmd("sh", "-c", "echo '' | docker compose -f ./fixtures/run-test/piped-test.yaml run --rm tty-test")
246+
res := icmd.RunCmd(cmd)
247+
248+
res.Assert(t, icmd.Expected{Out: "No TTY detected"})
249+
res.Assert(t, icmd.Success)
250+
})
251+
252+
t.Run("compose run piped input with explicit --tty should fail", func(t *testing.T) {
253+
if composeStandaloneMode {
254+
t.Skip("Skipping test compose with piped input detection in standalone mode")
255+
}
256+
// Test that explicitly requesting TTY with piped input fails with proper error message
257+
// This should trigger the "input device is not a TTY" error
258+
cmd := c.NewCmd("sh", "-c", "echo 'test' | docker compose -f ./fixtures/run-test/piped-test.yaml run --rm --tty piped-test")
259+
res := icmd.RunCmd(cmd)
260+
261+
res.Assert(t, icmd.Expected{
262+
ExitCode: 1,
263+
Err: "the input device is not a TTY",
264+
})
265+
})
266+
267+
t.Run("compose run piped input with --no-TTY=false should fail", func(t *testing.T) {
268+
if composeStandaloneMode {
269+
t.Skip("Skipping test compose with piped input detection in standalone mode")
270+
}
271+
// Test that explicitly disabling --no-TTY (i.e., requesting TTY) with piped input fails
272+
// This should also trigger the "input device is not a TTY" error
273+
cmd := c.NewCmd("sh", "-c", "echo 'test' | docker compose -f ./fixtures/run-test/piped-test.yaml run --rm --no-TTY=false piped-test")
274+
res := icmd.RunCmd(cmd)
275+
276+
res.Assert(t, icmd.Expected{
277+
ExitCode: 1,
278+
Err: "the input device is not a TTY",
279+
})
280+
})
225281
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
services:
2+
piped-test:
3+
image: alpine
4+
command: cat
5+
# Service that will receive piped input and echo it back
6+
tty-test:
7+
image: alpine
8+
command: sh -c "if [ -t 0 ]; then echo 'TTY detected'; else echo 'No TTY detected'; fi"
9+
# Service to test TTY detection

0 commit comments

Comments
 (0)