Skip to content

compose: add attach command - #5302

Open
cvas22 wants to merge 5 commits into
containerd:mainfrom
cvas22:feat/compose-attach
Open

cvas22 wants to merge 5 commits into
containerd:mainfrom
cvas22:feat/compose-attach

Conversation

@cvas22

@cvas22 cvas22 commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

I added nerdctl compose attach, one more Compose command listed as unimplemented in the command reference (tracked in #3867).

It follows docker compose attach:

  • nerdctl compose attach SERVICE attaches to the service's container by running nerdctl attach on it, like compose exec runs nerdctl exec.
  • --index selects the container when the service has several instances (default 1).
  • --detach-keys and --no-stdin are passed through to nerdctl attach.
  • Like nerdctl attach, it needs a container created with stdin_open: true and tty: true. Otherwise the container logs through the logging binary and there is no fifo to attach to. compose up runs such a service in the foreground, like nerdctl run -it, so you detach from it with ctrl-p ctrl-q before attaching. I noted both in the command reference.

To avoid duplicating the container lookup, I moved it from Composer.Exec into a serviceContainer helper that both commands use. Exec's behavior and error messages are unchanged.

Changes:

  • pkg/composer/attach.go: Composer.Attach
  • pkg/composer/exec.go: the shared serviceContainer helper
  • cmd/nerdctl/compose/compose_attach.go: the command
  • cmd/nerdctl/compose/compose_attach_linux_test.go: tests for an out-of-range index, and for attaching to a service with stdin_open and tty through a pseudo-TTY (start the project under a pseudo-TTY and detach from compose up, then attach, send a command, check its output and detach with ctrl-p ctrl-q), following TestAttach
  • docs/command-reference.md: documented the command and removed it from the unimplemented list

Notes

I ran the compose integration tests locally, rootful, in a privileged container with nerdctl, containerd, runc, CNI and BuildKit built from this branch (docker buildx build --target out-full):

$ go test -count=1 -v -run TestComposeAttach ./cmd/nerdctl/compose/ -args -test.target=nerdctl
--- PASS: TestComposeAttach (18.86s)
    --- PASS: TestComposeAttach/rejects_an_index_out_of_range (0.04s)
    --- PASS: TestComposeAttach/attaches_to_the_service_container (3.01s)

It passed in three runs in a row. Running the whole ./cmd/nerdctl/compose/ package found one problem in an earlier version of this PR, which I fixed: starting the service with run -dit leaves nothing to attach to (failed to open stdout fifo), so the test now detaches from compose up instead. With that earlier version the other 77 compose tests passed. Rootless is only covered by CI.

Like #5300 and #5301, this adds a line to the compose command list, so a trivial rebase may be needed depending on merge order.

@cvas22

cvas22 commented Oct 31, 2026

Copy link
Copy Markdown
Contributor Author

The failing jobs aren't failing in TestComposeAttach. The almalinux-8 and linux jobs fail on image pulls (429 Too Many Requests / toomanyrequests: Data limit exceeded), and the rootless jobs on buildkitd not being reachable. I don't have permission to re-run the jobs, so could a maintainer re-run the failed ones?

@cvas22
cvas22 force-pushed the feat/compose-attach branch from 36be14c to 76b115f Compare November 4, 2026 02:13
@AkihiroSuda

Copy link
Copy Markdown
Member

Needs rebasing

Add nerdctl compose attach, like docker compose attach: it attaches to
the container of a service by running nerdctl attach on it. --index
selects the container when the service has several instances, and
--detach-keys and --no-stdin are passed to nerdctl attach.

Like nerdctl attach, it needs a container created with stdin_open and
tty. compose up runs such a service in the foreground, like
nerdctl run -it, so the test detaches from it with the detach keys.

The container lookup is moved from Exec into a serviceContainer helper
shared by both commands.

Signed-off-by: Srinivas Chippagiri <cvas22@gmail.com>
@cvas22
cvas22 force-pushed the feat/compose-attach branch from 76b115f to 2695bab Compare November 4, 2026 07:51
@cvas22

cvas22 commented Nov 4, 2026

Copy link
Copy Markdown
Contributor Author

Rebased on main after #5301 merged; the only conflict was the command registration.

Signed-off-by: Srinivas Chippagiri <cvas22@gmail.com>
@cvas22

cvas22 commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

The almalinux-8 failure was my test. Attach took more than 3s to connect there, so the command and the detach keys arrived together. a25d232 waits before sending the command too.

Waiting before sending the command made the attach test lose the
output in the in-host jobs, so go back to feeding it right away like
TestAttach does.

Signed-off-by: Srinivas Chippagiri <cvas22@gmail.com>
@cvas22

cvas22 commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

Reverted that wait in 78b2a6a, it made the attach test lose its output on the in-host jobs. The test now matches the version that passed the test workflow, and the almalinux-8 job is in the flaky workflow.

compose attach loads the project before it runs nerdctl attach, which
can take several seconds on slow runners, and input that is pending
together with the detach keys when nerdctl attach starts can get lost.
Send the command once a second for a while before detaching, so a
later one reaches the container whenever attach is up.

Signed-off-by: Srinivas Chippagiri <cvas22@gmail.com>
@cvas22

cvas22 commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

The revert still failed on the rootless jobs, so 51ca1c9 sends the command once a second for a while before detaching. When compose takes a few seconds to start nerdctl attach, the input and the detach keys end up racing, like the note in TestAttach says.

Signed-off-by: Srinivas Chippagiri <cvas22@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants