Skip to content

compose: add attach command - #5302

Open
cvas22 wants to merge 1 commit into
containerd:mainfrom
cvas22:feat/compose-attach
Open

cvas22 wants to merge 1 commit 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.

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 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?

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