Skip to content
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

feat: add 'nerdctl container attach' #2108

Merged
merged 1 commit into from
Aug 2, 2023

Conversation

davidhsingyuchen
Copy link
Contributor

@davidhsingyuchen davidhsingyuchen commented Mar 16, 2023

Summary

PR is unblocked by #2128. After this PR, for a container started in attached mode, a user can detach from it (enabled by #2128) and then re-attach to it using nerdctl attach (enabled by this PR).

However, since "dual logging" is not implemented yet, containers spun up in detached mode, namely via nerdctl run -d or nerdctl start (without -a), cannot be nerdctl attached, which is mentioned in nerdctl attach --help and command-reference.md. To be specific, for a container started in detached mode, cio.LogURI instead of cio.NewCreator is used, which means the FIFOs that store the container's stdin/stdout/stderr and will be used by cio.NewAttach won't be there.

Notes

  • ContainerAttachOptions.GOptions is not used by container.Attach at the moment, but I still added it for consistency across commands.
  • To keep this PR as simple as possible, only the bare minimum CLI parameters are supported in it. In other words, out of all the parameters supported by docker attach, only --detach-keys is supported, and both --no-stdin and sig-proxy are not supported yet. We can add support for them in follow-up PRs.
  • cio.Terminal is not really used by cio.NewAttach (i.e., whether the container is with a terminal is already decided when the container is created/started), so I omit the logic to decide if it should be passed to cio.NewAttach.
  • Currently only one attach session is supported because only one reader can be attached to a task under containerd's current design. Supporting multiple attach sessions at nerdctl side implies quite a bit complexity, so maybe we can leave it to another PR, or even consider doing it at containerd side.

@davidhsingyuchen davidhsingyuchen force-pushed the add-attach branch 2 times, most recently from 64efeff to 4441ce2 Compare June 12, 2023 21:48
@davidhsingyuchen davidhsingyuchen changed the title [WIP] feat: add 'nerdctl container attach' feat: add 'nerdctl container attach' Jun 12, 2023
@davidhsingyuchen davidhsingyuchen marked this pull request as ready for review June 12, 2023 22:50
@davidhsingyuchen
Copy link
Contributor Author

@AkihiroSuda PTAL, thanks! Some jobs seemed to be cancelled for no obvious reason.

@AkihiroSuda AkihiroSuda added this to the v1.5.0 milestone Jun 13, 2023
.gitignore Outdated
@@ -7,3 +7,6 @@ _output

# vagrant
/.vagrant

# IDE
.vscode
Copy link
Member

Choose a reason for hiding this comment

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

This should be in your local git config

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 for the pointer! TIL 😄

Args: cobra.ExactArgs(1),
Short: "Attach stdin, stdout, and stderr to a running container.\n\nCaveats:\n\n" +
"- Currently only one attach session is allowed.\n" +
"- Before [dual logging](https://github.com/containerd/nerdctl/issues/1946#issuecomment-1443649307) is implemented, " +
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"- Before [dual logging](https://github.com/containerd/nerdctl/issues/1946#issuecomment-1443649307) is implemented, " +
"- Until dual logging (issue #1946) is implemented, " +

)

// skipAttachForDocker should be called by attach-related tests that assert 'read detach keys' in stdout.
func skipAttachForDocker(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

Needs t.Helper()

func prepareContainerToAttach(base *testutil.Base, containerName string) {
opts := []func(*testutil.Cmd){
testutil.WithStdin(testutil.NewDelayOnceReader(bytes.NewReader(
[]byte{16, 17}, // https://www.physics.udel.edu/~watson/scen103/ascii.html
Copy link
Member

Choose a reason for hiding this comment

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

Please add a comment to explain the key codes (C-p C-q)

@davidhsingyuchen
Copy link
Contributor Author

@AkihiroSuda All the comments should have been addressed, thanks!

@AkihiroSuda AkihiroSuda requested a review from ktock July 25, 2023 18:51
@AkihiroSuda
Copy link
Member

Sorry needs rebase

@davidhsingyuchen
Copy link
Contributor Author

@AkihiroSuda done!

Comment on lines 32 to 35
Short: "Attach stdin, stdout, and stderr to a running container.\n\nCaveats:\n\n" +
"- Currently only one attach session is allowed.\n" +
"- Until dual logging (issue #1946) is implemented, " +
"a container that is spun up by either `nerdctl run -d` or `nerdctl start` (without `--attach`) cannot be attached to.",
Copy link
Member

Choose a reason for hiding this comment

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

Can we use multi-line quote?

`aaa
bbb`

Use: "attach [flags] CONTAINER",
Args: cobra.ExactArgs(1),
Short: "Attach stdin, stdout, and stderr to a running container.\n\nCaveats:\n\n" +
"- Currently only one attach session is allowed.\n" +
Copy link
Member

Choose a reason for hiding this comment

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

Should mention that what happens when the second session tries to attach.

Short: "Attach stdin, stdout, and stderr to a running container.\n\nCaveats:\n\n" +
"- Currently only one attach session is allowed.\n" +
"- Until dual logging (issue #1946) is implemented, " +
"a container that is spun up by either `nerdctl run -d` or `nerdctl start` (without `--attach`) cannot be attached to.",
Copy link
Member

Choose a reason for hiding this comment

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

Should list some examples of containers allowed to be attached.

Caveats:

- Currently only one attach session is allowed.
- Until dual logging (issue #1946) is implemented, a container that is spun up by either `nerdctl run -d` or `nerdctl start` (without `--attach`) cannot be attached to.
Copy link
Member

Choose a reason for hiding this comment

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

Can we have examples of containers allowed to be attached?

Signed-off-by: Hsing-Yu (David) Chen <davidhsingyuchen@gmail.com>
Copy link
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

@AkihiroSuda AkihiroSuda merged commit 9cd00de into containerd:main Aug 2, 2023
20 of 21 checks passed
@davidhsingyuchen davidhsingyuchen deleted the add-attach branch August 2, 2023 16:53
@AkihiroSuda AkihiroSuda mentioned this pull request Mar 13, 2024
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