Skip to content
Closed
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
43 changes: 43 additions & 0 deletions cmd/nerdctl/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ package main

import (
"context"
"encoding/json"
"errors"
"fmt"
"net/url"

"github.com/containerd/console"
"github.com/containerd/typeurl"
"github.com/opencontainers/runtime-spec/specs-go"

"github.com/containerd/containerd"
"github.com/containerd/containerd/cio"
"github.com/containerd/nerdctl/pkg/formatter"
Expand Down Expand Up @@ -95,13 +101,50 @@ func startContainer(ctx context.Context, container containerd.Container) error {
logrus.WithError(err).Debug("failed to delete old task")
}
}
flagT, err := getFlagT(ctx, container)
if err != nil {
return err
}
if flagT {
var con console.Console
con = console.Current()
defer con.Reset()
if err := con.SetRaw(); err != nil {
return fmt.Errorf("failed to SetRaw,err is: %w", err)
}
if con == nil {
return errors.New("got nil con with flagT=true")
}
taskCIO = cio.NewCreator(cio.WithStreams(con, con, nil), cio.WithTerminal)
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

For consistency with Docker, we have to detach tty

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

what should I do? change from return errors.New("got nil con with flagT=true") to return errors.New("got nil con")?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Probably you should attach a dummy terminal, not the real terminal, to emulate Docker's behavior.
Needs further investigation.

task, err := container.NewTask(ctx, taskCIO)
if err != nil {
return err
}
return task.Start(ctx)
}

func getFlagT(ctx context.Context, container containerd.Container) (bool, error) {
info, err := container.Info(ctx)
if err != nil {
return false, err
}
spec, err := typeurl.UnmarshalAny(info.Spec)
if err != nil {
return false, fmt.Errorf("failed to UnmarshalAny %s,err is: %w", info.Spec, err)
}
specInfo, err := json.Marshal(spec)
if err != nil {
return false, fmt.Errorf("failed to Marshal %s,err is: %w", spec, err)
}
var s specs.Spec
err = json.Unmarshal(specInfo, &s)
if err != nil {
return false, fmt.Errorf("failed to Unmarshal %s,err is: %w", string(specInfo), err)
}
return s.Process.Terminal, nil
}

func startShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
// show non-running container names
statusFilterFn := func(st containerd.ProcessStatus) bool {
Expand Down