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 configurable escape key for attach/exec #15666

Merged
merged 1 commit into from Jan 3, 2016

Conversation

vdemeester
Copy link
Member

@vdemeester vdemeester commented Aug 18, 2015

Implement configurable escape keys (for attach, exec, run and start) using the client-side configuration and/or flags --detach-keys. This takes some pieces from #6460 from @vieux β€” except that this time it uses the client configuration (~/.docker/config.json for example). 🐧

This PR allows you to change the set of keys you have to type to detach in TTY mode, it can be less or more that two.
The basic ASCII table is supported, for example you can do: docker -d --detach-keys="ctrl-@" and detach in one keystroke or docker -d --detach-keys="DEL,+" if you don't like to have a ctrl key

The configuration looks like this :

{
    "detachKeys": "ctrl-a,a"
}

The default keybinding stays the same : ctrl-p ctrl-q but it makes possible to configure something else.

There is room for improvements on the code that should be address during the design review process I think :

  • For attach command I use queryString (detachKeys=ctrl-a%2Ca) but for exec command I used runConfig.ExecConfig. That felt weird and it's not coherent, it should be the same thing for both. It could maybe be passed as HTTP Header, I don't know πŸ˜… β€” that's why I'm putting this in review.
  • Right now, I don't validate the configuration client side, so if there is something wrong with the detachKeys passed, It's just logged and the defaults one are used.
  • Some refactoring is there is a little too much duplicated code for my taste (the keys, err = term.ToBytes(detachKeys) part is repeated few times..).
  • Probably add a json:omitempty for DetachKeys on runconfig.ExecConfig.

What is still to do :

  • Rebase it πŸ˜…
  • Implement flags by commands (run, start, attach, exec)
  • Write some integration tests.
  • Update some documentation.
  • Update the commit message to make it more clear πŸ˜….

🐸

Fixes #3519.

Signed-off-by: Vincent Demeester vincent@sbr.pm

@crquan
Copy link
Contributor

crquan commented Aug 29, 2015

πŸ‘

@vdemeester vdemeester force-pushed the 3519-configurable-escape branch 3 times, most recently from e738927 to 678fe2d Compare September 1, 2015 19:29
@tiborvass
Copy link
Contributor

A question to answer is if there are already configs in the client config.json that have no flag equivalents, and if not, then is that something we want ? IOW, if we add a config in config.json, should we also add a flag?

@vdemeester
Copy link
Member Author

@tiborvass good question 🐱 I'm wondering what should be the flag then πŸ˜….

/cc @duglin @thaJeztah @runcom 😝

@runcom
Copy link
Member

runcom commented Sep 23, 2015

if we add a config in config.json, should we also add a flag

if I'm understanding correctly your question, another question is, what's the use case for having this defined as a flag? do really ppl run their containers assigning different escape keys? why would they?
docker run -ti --escape-keys "ctrl-a,a" my_cont0 bash
docker exec -ti --escape-keys "ctrl-b,b" my_cont1 bash
I don't like it, but maybe I don't see the value in this. So, I think even if config.json doesn't have configurations w/o an equivalent flag I still see this option better suited in the json instead of a command flag

(notificed the original issue is for run command as well)

@vdemeester
Copy link
Member Author

@runcom talking with @tiborvass a bit on IRC, there might be usecase for a one-off different escape keys : when doing dind and need to detach from the inner container without detaching from the outer one β€” editing the config file inside the outer container is not that convenient. A flag would be handy there πŸ˜…

@runcom
Copy link
Member

runcom commented Sep 23, 2015

Ok that's the use case I haven't thought about :)

@tiborvass
Copy link
Contributor

As an addendum, I don't think this should be on exec, only run and possibly attach. I wonder about start too (when doing start -ai)

@vdemeester
Copy link
Member Author

@tiborvass you're talking only about the flag right ? (for not having it on exec)

@tiborvass
Copy link
Contributor

@vdemeester i just realized we still allow detaching from exec, that's a mistake. I don't know what to do then.

@vdemeester
Copy link
Member Author

@tiborvass hum you think we should disable detaching from exec no matter this PR ? I kinda agree on this but pretty sure this should be another PR (probably to be merged before that one). I can take a look πŸ˜‰

@tiborvass
Copy link
Contributor

@vdemeester I agree that issue is a separate one :)

@rhatdan
Copy link
Contributor

rhatdan commented Sep 30, 2015

I would want to change this to be system default.
Is there an easy way to cause the default config.json to have different defaults?

@vdemeester
Copy link
Member Author

@rhatdan what do you mean by that ?

@rhatdan
Copy link
Contributor

rhatdan commented Sep 30, 2015

I would want to change all containers to run with a different detach command. Something like what tmux does. I want to set it in a config or on the daemon and be done with it. I don't want to think about if for every docker run command.

http://superuser.com/questions/74492/whats-the-best-prefix-escape-sequence-for-screen-or-tmux

@vdemeester
Copy link
Member Author

@rhatdan well this is the goal of this PR : to add a config options (on client side, on ~/.docker/config.json) so that by default it uses something else, and an option (yet to be written/updated) to be able to do it per containers. So this would answer your use case then (the config part of it)

@thaJeztah
Copy link
Member

damn you github, why are you removing/adding the wrong labels 😒

Implement configurable detach keys (for `attach`, exec`, `run` and
`start`) using the client-side configuration

- Adds a `--detach-keys` flag to `attach`, `exec`, `run` and `start`
  commands.
- Adds a new configuration field (in `~/.docker/config.json`) to
  configure the default escape keys for docker client.

Signed-off-by: Vincent Demeester <vincent@sbr.pm>
@thaJeztah
Copy link
Member

It's green πŸŽ‰ \o/

thaJeztah added a commit that referenced this pull request Jan 3, 2016
Implement configurable escape key for attach/exec
@thaJeztah thaJeztah merged commit db738dd into moby:master Jan 3, 2016
@vdemeester
Copy link
Member Author

πŸŽ‰ πŸŽ…

@pulserdd
Copy link

pulserdd commented May 6, 2016

Hi, I have a question on using this detachKeys config. We are using ecs instance on which these docker tasks are run. Following the documentation, I added the "detachKeys": "ctrl-e" to /root/.docker/config.json but it does not seem to take effect. I still have to use ctrl-c to exit on attach and that kills the container. Could someone share some pointers on resolving this? I have tried restarting the docker daemon but to no avail. Would highly appreciate some help. Thanks.

@dryfish
Copy link

dryfish commented May 9, 2016

@pulserdd I've just tested with 1.11.1 and "DetachKeys": "ctrl-x" is working for me. I think docker JSON files always use title casing.

@thaJeztah
Copy link
Member

hm, in that case either the documentation is incorrect, or something changed in the parsing of that file https://docs.docker.com/engine/reference/commandline/cli/#configuration-files

@vdemeester ?

@dryfish
Copy link

dryfish commented May 9, 2016

Ah. I've been looking at docker-machine configs recently and seeing title case.

I've just tried docker run with detachKeys and that also works for me.

(ctrl-e,e and ctrl-x,x working for me when put into ~/.docker/config.json for the user running docker run. Testing with an image running /bin/bash; type a command; ctrl-p (once) to get it back from history; ctrl-x x to detach.)

@vdemeester
Copy link
Member Author

@pulserdd tried it out and work as expected. In the config file it takes both detachKeys or DetachKeys. As @dryfish it has to be set in $HOME/.docker/config.json of the user that do the docker run, or docker attach, or … It's client-side so no need to restart the daemon or anything.

@pulserdd
Copy link

pulserdd commented May 9, 2016

Thanks for your replies. I tried to follow your comments but still the same issue. Here's what I have in config
[root@ip-1-2-3-4 ec2-user]# cat ~/.docker/config.json { "auths": { "quay.io": { "auth": "xxx", "email": "" } }, "DetachKeys": "ctrl-e" }

Result:
[root@ip-1-2-3-4 ec2-user]# docker attach 7fe7a4ed6213 E E Ee C2016-05-09 18:06:05,463 INFO [cs.iosmdm.iosmdm] wsgi exiting

my docker version
Docker version 1.9.1, build a34a1d5/1.9.1

@vdemeester
Copy link
Member Author

@pulserdd oh it's normal then, this feature is only available starting docker 1.10…

@pulserdd
Copy link

pulserdd commented May 9, 2016

oh ok sorry I missed that while following the docs. Thanks once again for a great community support.

@MhdSyrwan
Copy link

@vdemeester Can you please correct the example in the first post since people sometimes refer to this pull request as a documentation page (like me πŸ˜„ ).

The wrong example:

{
    "detachKey": "ctrl-a,a"
}

The correct one:

{
    "detachKeys": "ctrl-a,a"
}

@thaJeztah
Copy link
Member

@MhdSyrwan thanks for letting us know! I updated the description

khollbach added a commit to khollbach/config-files that referenced this pull request Jan 16, 2019
By default, docker has "detach from container" bound to C-p C-q,
so C-p will behave as "sticky" when used in docker since it's waiting
to see if you type C-q.

We rebind it to something less intrusive: C-@

See https://stackoverflow.com/questions/41820108/ctrl-p-and-ctrl-n-behaving-unexpectedly-under-docker
See also moby/moby#15666
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.

None yet