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

Implement docker compose attach #11181

Merged

Conversation

g0t4
Copy link
Contributor

@g0t4 g0t4 commented Nov 11, 2023

What I did

I implemented docker compose attach by hooking into the underlying docker container attach using it's NewAttachCommand with some overrides to support lookup of service name/index => container ID.

Usage:

docker compose attach foo
# STDIN/STDOUT/ERR connected

In my case, I had a development environment that uses dotnet watch inside of a container (started with compose). And then I needed to send Ctrl+R to reload my server (as needed). And thus the need to attach to the container's STDIN.

@g0t4
Copy link
Contributor Author

g0t4 commented Nov 11, 2023

@ndeloof

@glours
Copy link
Contributor

glours commented Nov 11, 2023

Hello @g0t4
Isn't the --attach from the up command enough for what you want to do? I mean did you try with docker compose up --attach foo ?
By the way there is already code to find and attach to a container see here for example, it should be better to re-use this existing code
Same for your Lookup function, everything is already available with the getSpecifiedContainer function

@ndeloof
Copy link
Contributor

ndeloof commented Nov 11, 2023

--attach require you know in advance which service you want to attach to, while docker compose attach allows to run "diagnostic" on an existing stack. This is basically for parity with docker attach`` by the way, my PR to export RunAttach` on docker/cli has been merged

opts.Service = args[0]
}

containerID, err := backend.LookupContainer(ctx, projectName, opts)
Copy link
Contributor

Choose a reason for hiding this comment

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

please mimic the exec commands, and have api.Backend to declare a new Attach command, then rely on docker/cli for implementation if relevant.
This for sure requires to duplicate flags definition, but those don't change much and this is a general UX-homogeneity issue we should address in a global way

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok so you would prefer that I not reuse NewAttachCommand and instead hook into RunAttach like exec does with RunExec?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And btw, if I hook into RunAttach I can easily add back the ability to docker compose attach (w/o service name) and have it grab the first container in the project. I removed that when using NewAttachCommand due to some friction with overriding validation.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes indeed, I'd prefer not to rely on NewAttachCommand here (sharing command and flags definition with docker CLI is a larger effort we need to investigate)

I can easily add back the ability to docker compose attach (w/o service name) and have it grab the first container in the project

please don't, containers in a project are not ordered, so this won't be deterministic. Better rely on the same selection mechanism used by compose exec to enforce an homogeneous UX

@g0t4
Copy link
Contributor Author

g0t4 commented Nov 13, 2023

Isn't the --attach from the up command enough for what you want to do? I mean did you try with docker compose up --attach foo ?

docker compose up --attach doesn't attach STDIN, in a perfect world I would prefer that it did but given the nature of multiple containers, I went the route of mirroring the underlying docker container attach command for a single service's container.

return err
}

// TODO? --dry-run? would it be helpful to print the matching container name/ID and not attach?
Copy link
Contributor

Choose a reason for hiding this comment

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

--dry-run apply to commands which could mutate the state of your stack

@g0t4 g0t4 force-pushed the 11153-compose-attach-override-container-attach-cmd branch from 95f0ea1 to 2480c44 Compare December 4, 2023 16:19
@g0t4
Copy link
Contributor Author

g0t4 commented Dec 4, 2023

@ndeloof ok I finally got a chance to rework things to use RunAttach, I had to update to docker/cli and docker/docker v1.25 beta 1

Let me know what you think.

@ndeloof
Copy link
Contributor

ndeloof commented Dec 4, 2023

nice! please amend your commit to sign them off, this is required for any contribution by legals.
I guess we could have a dedicated PR to bump docker/cli to v1.25 beta.1

@g0t4 g0t4 force-pushed the 11153-compose-attach-override-container-attach-cmd branch from c7a93d4 to ccbe733 Compare December 4, 2023 17:29
@g0t4
Copy link
Contributor Author

g0t4 commented Dec 4, 2023

done... rebased with sign off

do you want me to open a separate PR for v1.25?

@g0t4
Copy link
Contributor Author

g0t4 commented Dec 4, 2023

@ndeloof here is the PR for bumping docker/cli: #11246

@g0t4 g0t4 force-pushed the 11153-compose-attach-override-container-attach-cmd branch from ccbe733 to 44e653d Compare December 11, 2023 06:00
@g0t4
Copy link
Contributor Author

g0t4 commented Dec 11, 2023

@ndeloof rebased

@ndeloof
Copy link
Contributor

ndeloof commented Dec 11, 2023

run make mocks to regenerate mocks according to API updates

@g0t4 g0t4 closed this Dec 22, 2023
@g0t4 g0t4 force-pushed the 11153-compose-attach-override-container-attach-cmd branch from 44e653d to e105f16 Compare December 22, 2023 17:33
@g0t4
Copy link
Contributor Author

g0t4 commented Dec 22, 2023

Ok I made a rebase mistake, brb with a fix.

@g0t4
Copy link
Contributor Author

g0t4 commented Dec 22, 2023

Ok, @ndeloof fixed (praise the reflog gods)... and ran make mocks

@g0t4 g0t4 reopened this Dec 22, 2023
@g0t4 g0t4 force-pushed the 11153-compose-attach-override-container-attach-cmd branch from 0da8c94 to 057ba8e Compare December 22, 2023 18:16
@g0t4
Copy link
Contributor Author

g0t4 commented Dec 22, 2023

signoff done too

Copy link

codecov bot commented Dec 22, 2023

Codecov Report

Attention: 36 lines in your changes are missing coverage. Please review.

Comparison is base (f659918) 56.57% compared to head (057ba8e) 56.21%.

❗ Current head 057ba8e differs from pull request most recent head 5f4b22e. Consider uploading reports for the commit 5f4b22e to get more accurate results

Files Patch % Lines
cmd/compose/attach.go 44.44% 19 Missing and 1 partial ⚠️
pkg/compose/attach_service.go 0.00% 11 Missing ⚠️
pkg/api/proxy.go 16.66% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #11181      +/-   ##
==========================================
- Coverage   56.57%   56.21%   -0.36%     
==========================================
  Files         134      137       +3     
  Lines       11454    11707     +253     
==========================================
+ Hits         6480     6581     +101     
- Misses       4351     4476     +125     
- Partials      623      650      +27     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ndeloof
Copy link
Contributor

ndeloof commented Jan 8, 2024

Please rebase and run make docs to update command line reference docs, then we can merge

…er attach)

Signed-off-by: Wes Higbee <wes.mcclure@gmail.com>
…rlying docker container attach

Signed-off-by: Wes Higbee <wes.mcclure@gmail.com>
Signed-off-by: Wes Higbee <wes.mcclure@gmail.com>
@g0t4 g0t4 force-pushed the 11153-compose-attach-override-container-attach-cmd branch from 057ba8e to dcf6bd7 Compare January 8, 2024 23:18
Signed-off-by: Wes Higbee <wes.mcclure@gmail.com>
@g0t4
Copy link
Contributor Author

g0t4 commented Jan 8, 2024

done!

Copy link
Contributor

@glours glours left a comment

Choose a reason for hiding this comment

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

LGTM

@glours glours enabled auto-merge January 11, 2024 10:07
@glours
Copy link
Contributor

glours commented Jan 11, 2024

We should add an e2e test as a follow up PR

@glours glours merged commit 24d3404 into docker:main Jan 11, 2024
26 checks passed
@g0t4
Copy link
Contributor Author

g0t4 commented Jan 26, 2024

@glours is there a similar e2e test in mind that you think would work well with attach?

matt9ucci added a commit to matt9ucci/DockerCompletion that referenced this pull request Apr 8, 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.

None yet

3 participants