diff --git a/cmd/nerdctl/container/container_exec.go b/cmd/nerdctl/container/container_exec.go index 4dd596ce54c..fc6f0c0c6ac 100644 --- a/cmd/nerdctl/container/container_exec.go +++ b/cmd/nerdctl/container/container_exec.go @@ -18,9 +18,7 @@ package container import ( "errors" - "os" - "github.com/moby/term" "github.com/spf13/cobra" containerd "github.com/containerd/containerd/v2/client" @@ -58,6 +56,7 @@ func NewExecCommand() *cobra.Command { } func processExecCommandOptions(cmd *cobra.Command) (types.ContainerExecOptions, error) { + // We do not check if we have a terminal here, as container.Exec calling console.Current will ensure that globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.ContainerExecOptions{}, err @@ -88,13 +87,6 @@ func processExecCommandOptions(cmd *cobra.Command) (types.ContainerExecOptions, } } - _, isTerminal := term.GetFdInfo(os.Stdin) - if !flagD { - if flagT && flagI && !isTerminal { - return types.ContainerExecOptions{}, errors.New("the input device is not a TTY") - } - } - workdir, err := cmd.Flags().GetString("workdir") if err != nil { return types.ContainerExecOptions{}, err diff --git a/cmd/nerdctl/container/container_run.go b/cmd/nerdctl/container/container_run.go index 143cd16d99f..a514d3795af 100644 --- a/cmd/nerdctl/container/container_run.go +++ b/cmd/nerdctl/container/container_run.go @@ -389,7 +389,10 @@ func runAction(cmd *cobra.Command, args []string) error { var con console.Console if createOpt.TTY && !createOpt.Detach { - con = console.Current() + con, err = consoleutil.Current() + if err != nil { + return err + } defer con.Reset() if err := con.SetRaw(); err != nil { return err diff --git a/pkg/cmd/container/attach.go b/pkg/cmd/container/attach.go index eb74252cf1d..fbbddafcb40 100644 --- a/pkg/cmd/container/attach.go +++ b/pkg/cmd/container/attach.go @@ -66,7 +66,10 @@ func Attach(ctx context.Context, client *containerd.Client, req string, options con console.Console ) if spec.Process.Terminal { - con = console.Current() + con, err = consoleutil.Current() + if err != nil { + return err + } defer con.Reset() if err := con.SetRaw(); err != nil { return fmt.Errorf("failed to set the console to raw mode: %w", err) diff --git a/pkg/cmd/container/exec.go b/pkg/cmd/container/exec.go index a7ad6857c33..c00c776998b 100644 --- a/pkg/cmd/container/exec.go +++ b/pkg/cmd/container/exec.go @@ -106,7 +106,10 @@ func execActionWithContainer(ctx context.Context, client *containerd.Client, con var con console.Console if options.TTY { - con = console.Current() + con, err = consoleutil.Current() + if err != nil { + return err + } defer con.Reset() if err := con.SetRaw(); err != nil { return err @@ -164,7 +167,11 @@ func generateExecProcessSpec(ctx context.Context, client *containerd.Client, con pspec := spec.Process pspec.Terminal = options.TTY if pspec.Terminal { - if size, err := console.Current().Size(); err == nil { + con, err := consoleutil.Current() + if err != nil { + return nil, err + } + if size, err := con.Size(); err == nil { pspec.ConsoleSize = &specs.Box{Height: uint(size.Height), Width: uint(size.Width)} } } diff --git a/pkg/consoleutil/consoleutil.go b/pkg/consoleutil/consoleutil.go index 4f9167a0b4c..a19988a6895 100644 --- a/pkg/consoleutil/consoleutil.go +++ b/pkg/consoleutil/consoleutil.go @@ -18,8 +18,22 @@ package consoleutil import ( "context" + "os" + + "github.com/containerd/console" ) +// Current is from https://github.com/containerd/console/blob/v1.0.4/console.go#L68-L81 +// adapted so that it does not panic +func Current() (c console.Console, err error) { + for _, s := range []*os.File{os.Stderr, os.Stdout, os.Stdin} { + if c, err = console.ConsoleFromFile(s); err == nil { + return c, nil + } + } + return nil, console.ErrNotAConsole +} + // resizer is from https://github.com/containerd/containerd/blob/v1.7.0-rc.2/cmd/ctr/commands/tasks/tasks.go#L25-L27 type resizer interface { Resize(ctx context.Context, w, h uint32) error diff --git a/pkg/containerutil/containerutil.go b/pkg/containerutil/containerutil.go index 3e1af0cb9e6..814156117fc 100644 --- a/pkg/containerutil/containerutil.go +++ b/pkg/containerutil/containerutil.go @@ -244,7 +244,10 @@ func Start(ctx context.Context, container containerd.Container, flagA bool, clie flagT := process.Process.Terminal var con console.Console if flagA && flagT { - con = console.Current() + con, err = consoleutil.Current() + if err != nil { + return err + } defer con.Reset() if err := con.SetRaw(); err != nil { return err