Skip to content

feat: add --no-stdin flag to container attach #4176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion cmd/nerdctl/container/container_attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package container

import (
"io"

"github.com/spf13/cobra"

containerd "github.com/containerd/containerd/v2/client"
Expand Down Expand Up @@ -56,6 +58,7 @@ Caveats:
SilenceErrors: true,
}
cmd.Flags().String("detach-keys", consoleutil.DefaultDetachKeys, "Override the default detach keys")
cmd.Flags().Bool("no-stdin", false, "Do not attach STDIN")
return cmd
}

Expand All @@ -68,9 +71,18 @@ func attachOptions(cmd *cobra.Command) (types.ContainerAttachOptions, error) {
if err != nil {
return types.ContainerAttachOptions{}, err
}
noStdin, err := cmd.Flags().GetBool("no-stdin")
if err != nil {
return types.ContainerAttachOptions{}, err
}

var stdin io.Reader
if !noStdin {
stdin = cmd.InOrStdin()
}
return types.ContainerAttachOptions{
GOptions: globalOptions,
Stdin: cmd.InOrStdin(),
Stdin: stdin,
Stdout: cmd.OutOrStdout(),
Stderr: cmd.ErrOrStderr(),
DetachKeys: detachKeys,
Expand Down
41 changes: 41 additions & 0 deletions cmd/nerdctl/container/container_attach_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,44 @@ func TestAttachForAutoRemovedContainer(t *testing.T) {

testCase.Run(t)
}

func TestAttachNoStdin(t *testing.T) {
testCase := nerdtest.Setup()

testCase.Cleanup = func(data test.Data, helpers test.Helpers) {
helpers.Anyhow("rm", "-f", data.Identifier())
}

testCase.Setup = func(data test.Data, helpers test.Helpers) {
cmd := helpers.Command("run", "-it", "--detach-keys=ctrl-p,ctrl-q", "--name", data.Identifier(),
testutil.CommonImage, "sleep", "5")
cmd.WithPseudoTTY()
cmd.Feed(bytes.NewReader([]byte{16, 17})) // Ctrl-p, Ctrl-q to detach (https://en.wikipedia.org/wiki/C0_and_C1_control_codes)
Copy link
Contributor

@apostasie apostasie May 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@subashkotha let's test my hypothesis that there is a bug in logging.

So, instead of sending cmd.Feed(bytes.NewReader([]byte{16, 17})) after one second, instead do your logs / wait on ready BEFORE sending the escape sequence.

eg:

cmd.Feeder(func()io.Reader{

for {
				out := helpers.Capture("logs", data.Identifier())
				if strings.Contains(out, "ready") {
					break
				}
}

return bytes.NewReader([]byte{16, 17})

}
)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pseudo-code ^ just add back the sleep and make this proper - thanks a lot! :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, let me try this out.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking logs during setup isn't reliable since the container isn't running yet. I’ve updated the test to avoid relying on logs altogether, especially given the known issue: #4233.

Instead, we now attach with --no-stdin, send input like xyz, and simply assert that xyz does not appear in the logs after attach.

cmd.Run(&test.Expected{
ExitCode: 0,
Output: func(stdout string, info string, t *testing.T) {
assert.Assert(t, strings.Contains(helpers.Capture("inspect", "--format", "{{.State.Running}}", data.Identifier()), "true"))
},
})
}

testCase.Command = func(data test.Data, helpers test.Helpers) test.TestableCommand {
cmd := helpers.Command("attach", "--no-stdin", data.Identifier())
cmd.WithPseudoTTY()
cmd.Feed(strings.NewReader("should-not-appear\n"))
cmd.Feed(bytes.NewReader([]byte{16, 17}))
return cmd
}

testCase.Expected = func(data test.Data, helpers test.Helpers) *test.Expected {
return &test.Expected{
ExitCode: 0, // Since it's a normal exit and not detach.
Output: func(stdout string, info string, t *testing.T) {
logs := helpers.Capture("logs", data.Identifier())
assert.Assert(t, !strings.Contains(logs, "should-not-appear"))
},
}
}

testCase.Run(t)
}
3 changes: 2 additions & 1 deletion docs/command-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -686,8 +686,9 @@ Usage: `nerdctl attach CONTAINER`
Flags:

- :whale: `--detach-keys`: Override the default detach keys
- :whale: `--no-stdin`: Do not attach STDIN

Unimplemented `docker attach` flags: `--no-stdin`, `--sig-proxy`
Unimplemented `docker attach` flags: `--sig-proxy`

### :whale: nerdctl container prune

Expand Down
10 changes: 7 additions & 3 deletions pkg/cmd/container/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"errors"
"fmt"
"io"

"golang.org/x/term"

Expand Down Expand Up @@ -114,9 +115,12 @@ func Attach(ctx context.Context, client *containerd.Client, req string, options
}
io.Cancel()
}
in, err := consoleutil.NewDetachableStdin(con, options.DetachKeys, closer)
if err != nil {
return err
var in io.Reader
if options.Stdin != nil {
in, err = consoleutil.NewDetachableStdin(con, options.DetachKeys, closer)
if err != nil {
return err
}
}
opt = cio.WithStreams(in, con, nil)
} else {
Expand Down
Loading