Skip to content

Support NewTerminal & Detached mode (nerdctl -t -d)#1642

Merged
AkihiroSuda merged 1 commit into
containerd:mainfrom
yzxiu:support-newterminal_detached_mode
Dec 20, 2022
Merged

Support NewTerminal & Detached mode (nerdctl -t -d)#1642
AkihiroSuda merged 1 commit into
containerd:mainfrom
yzxiu:support-newterminal_detached_mode

Conversation

@yzxiu
Copy link
Copy Markdown
Contributor

@yzxiu yzxiu commented Dec 12, 2022

1 support run -t -d

nerdctal run --name demo -t -d imagename

2 support create -t

nerdctl create --name demo -t imagename
nerdctl start demo

3 support start -a to attach a terminal container

nerdctl create --name demo -t imagename
nerdctl start -a demo

4 support start/restart the container that run with -it (or without -d)

nerdctl run --name demo -it imagename
ctrl + c to stop demo
nerdctl start/restart demo

Signed-off-by: yaozhenxiu 946666800@qq.com

@AkihiroSuda
Copy link
Copy Markdown
Member

Thanks, but how to test?

@yzxiu
Copy link
Copy Markdown
Contributor Author

yzxiu commented Dec 12, 2022

Thanks, but how to test?

This requires building a container that only works properly with -t -d (or -t) modes
I have tested it locally, and I will add some integration test

@yzxiu yzxiu changed the title Fix: Support NewTerminal & Detached mode (nerdctl -t -d ...) Support NewTerminal & Detached mode (nerdctl -t -d ...) Dec 12, 2022
@yzxiu yzxiu force-pushed the support-newterminal_detached_mode branch 4 times, most recently from f907bf5 to 5438991 Compare December 12, 2022 07:53
@yzxiu
Copy link
Copy Markdown
Contributor Author

yzxiu commented Dec 12, 2022

@AkihiroSuda PTAL

@yzxiu yzxiu force-pushed the support-newterminal_detached_mode branch 2 times, most recently from 1df53f0 to af918c0 Compare December 12, 2022 09:06
@yzxiu yzxiu changed the title Support NewTerminal & Detached mode (nerdctl -t -d ...) Support NewTerminal & Detached mode (nerdctl -t -d) Dec 12, 2022
@yzxiu
Copy link
Copy Markdown
Contributor Author

yzxiu commented Dec 16, 2022

@fahedouch @djdongjin PTAL

Comment thread cmd/nerdctl/create_linux_test.go Outdated
if runtime.GOOS == "windows" {
t.Skip("buildkit is not enabled on windows, this feature may work on windows.")
}
testutil.DockerIncompatible(t)
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.

Why incompatible?

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.

Why incompatible?

using -d will use the json-file log driver and cause the windows test fail

Comment thread cmd/nerdctl/run_test.go Outdated

// this container only works properly with -t -d (or -t) modes
const TerminalContainerDockerfile = `
FROM golang:latest as builder
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.

Please pin the golang version and go.sum.

Also, can we avoid using golang here?

Copy link
Copy Markdown
Contributor Author

@yzxiu yzxiu Dec 16, 2022

Choose a reason for hiding this comment

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

Please pin the golang version and go.sum.

Also, can we avoid using golang here?

We can package the container in advance and just provide the image name, what do you think?

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.

I'd prefer to minimize dependency on third party images unless it is unavoidable

Copy link
Copy Markdown
Contributor Author

@yzxiu yzxiu Dec 16, 2022

Choose a reason for hiding this comment

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

I thought of an easier way, just run the container directly (bash or /bin/sh), this requires -t to run properly,
no need to build image anymore

Comment thread cmd/nerdctl/run_test.go Outdated
Comment thread cmd/nerdctl/run_test.go Outdated
@AkihiroSuda
Copy link
Copy Markdown
Member

AkihiroSuda commented Dec 16, 2022

Let me merge #1639 before this, sorry

Comment thread cmd/nerdctl/run_test.go Outdated
}
return nil
})
base.Cmd("container", "rm", "-f", containerName).AssertOK()
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.

This line can be a defer and moved up to before or after line 506, so that the rm -f containerName is always called.

Right now if either 507 or 517 fails, the container won't be cleaned.

Copy link
Copy Markdown
Contributor Author

@yzxiu yzxiu Dec 16, 2022

Choose a reason for hiding this comment

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

Here are actually two little tests, with "-t" and without "-t", so the container must be deleted before running again.
Perhaps use different name? or test separately?

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.

I think you can use different names and defer both rm -f containerName calls.

Same for the test in cmd/nerdctl/create_linux_test.go

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.

I think you can use different names and defer both rm -f containerName calls.

Same for the test in cmd/nerdctl/create_linux_test.go

Great, thanks

@AkihiroSuda AkihiroSuda added this to the v1.2.0 (tentative) milestone Dec 16, 2022
@yzxiu yzxiu force-pushed the support-newterminal_detached_mode branch from 333e9ed to e0ef207 Compare December 16, 2022 19:04
Comment thread cmd/nerdctl/run_test.go Outdated
assert.Check(t, strings.Contains(log, "bar"))
}

func TestNewTerminalDetachedMode(t *testing.T) {
Copy link
Copy Markdown
Member

@djdongjin djdongjin Dec 17, 2022

Choose a reason for hiding this comment

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

This test failed on windows. Not sure if it's due to some issue or not supported on windows.

Also suggest to include Run in the func name, something like TestRunWithTtyAndDetached.

Copy link
Copy Markdown
Contributor Author

@yzxiu yzxiu Dec 17, 2022

Choose a reason for hiding this comment

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

This test failed on windows. Not sure if it's due to some issue or not supported on windows.

it seems that the error location is different every time

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.

This test failed on windows. Not sure if it's due to some issue or not supported on windows.

using -d will use the json-file log driver and cause the windows test fail

@yzxiu yzxiu force-pushed the support-newterminal_detached_mode branch 8 times, most recently from 5099881 to 61e4f19 Compare December 17, 2022 08:53
@yzxiu yzxiu force-pushed the support-newterminal_detached_mode branch from 61e4f19 to 09b37d5 Compare December 17, 2022 09:51
@yzxiu yzxiu requested a review from AkihiroSuda December 17, 2022 14:50
Comment thread cmd/nerdctl/create_linux_test.go Outdated
defer base.Cmd("container", "rm", "-f", withTerminalContainerName).AssertOK()
withTerminalContainer := base.InspectContainer(withTerminalContainerName)
assert.Equal(base.T, "running", withTerminalContainer.State.Status)
assert.Equal(base.T, true, withTerminalContainer.State.Running)
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.

The test has to verify that tty is allocated when -t is specified.

Probably you can just test stty

$ docker run -d --name without-t alpine stty
f6ee65e70c512ddba7846f595f3dc5ce0cdb72110e9f901d1661e05f0a8a1372
$ docker logs without-t 
stty: standard input: Not a tty
$ docker container inspect without-t  | jq .[0].State.ExitCode
1
$ docker run -td --name with-t alpine stty
5b7bf5c0b3b07af1ccbe2c049aa34841ea635e10c0b493e056201e16dd38de03
$ docker logs with-t 
speed 38400 baud; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
-brkint -imaxbel
$ docker container inspect with-t  | jq .[0].State.ExitCode
0

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.

The test has to verify that tty is allocated when -t is specified.

Probably you can just test stty

$ docker run -d --name without-t alpine stty
f6ee65e70c512ddba7846f595f3dc5ce0cdb72110e9f901d1661e05f0a8a1372
$ docker logs without-t 
stty: standard input: Not a tty
$ docker container inspect without-t  | jq .[0].State.ExitCode
1
$ docker run -td --name with-t alpine stty
5b7bf5c0b3b07af1ccbe2c049aa34841ea635e10c0b493e056201e16dd38de03
$ docker logs with-t 
speed 38400 baud; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
-brkint -imaxbel
$ docker container inspect with-t  | jq .[0].State.ExitCode
0

done

Signed-off-by: yaozhenxiu <946666800@qq.com>
@yzxiu yzxiu force-pushed the support-newterminal_detached_mode branch from e45ce4d to c67b102 Compare December 17, 2022 16:14
Copy link
Copy Markdown
Member

@AkihiroSuda AkihiroSuda left a comment

Choose a reason for hiding this comment

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

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants