docker change Ctrl+p to something else?

Docker

Docker Problem Overview


I am using docker run /bin/bash to develop my container and every time I want to use Ctrl+p in a terminal or in emacs, I have to type it twice, since docker uses it to detach from a container (Ctrl+p Ctrl+q).

How can I change Ctrl+p to something else more convenient that is not used in emacs or in a terminal setting?

Docker Solutions


Solution 1 - Docker

Docker has a configuration file and you can change the detach binding by adding

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

to ~/.docker/config.json.

If there are other entries in config.json then just add the "detachKeys" entry as the last one. For example:

{
    "HttpHeaders": {
        "User-Agent": "Docker-Client/19.03.11 (linux)"
    },
    "detachKeys": "ctrl-z,z"
}

Note: If you are running docker using sudo docker ... the .docker directory with the configuration file must be in the root's home directory (i.e., /root/.docker/config.json).

Solution 2 - Docker

There is now a solution to this so thought I would update it here for others' convenience.

Just add a ~/.docker/config.json and set your own keybinding.

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

Now you can use Ctrl-p in bash and emacs again. Yeah!

Solution 3 - Docker

Here's what worked for me (with a bit more detail than the other answers)

You modify the docker config file:

~/.docker/config.json

For example:

{
    "auths": {
            "amz": {
                "auth": key"
            },
            "amz2": {
                "auth": key2"
            },
            "amz3": {
                "auth": "key3" }
         },
    "detachKeys": "ctrl-e,e"
}

NOTE: the detach is no longer ctrl-p,ctrl-q, but rather ctrl-e + e key.

So the steps are:

  1. Change the config file
  2. Detach from the terminal (using the old/default key bindings)
  3. Attach again (docker exec -it /bin/bash

Subsequently the new keybindings that you specified should work

Source: https://github.com/mx4492/dotfiles/commit/bad340b8ddeda6078093e89acacfcba8af74a0cc

Solution 4 - Docker

If anyone still can't get Ctrl-P to work inside a container even after changing the detach keys and calling Ctrl-P just prints out ^P in the terminal instead of going up an entry in your history, make sure the shell you're using in the container can actually handle the process signals.

E.g. instead of docker run -it ... sh.

Use docker run -it ... bash.

Solution 5 - Docker

To use this without changing global configuration

docker exec --detach-keys='ctrl-e,e' -ti foo /bin/bash

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Question719016View Question on Stackoverflow
Solution 1 - DockercreackView Answer on Stackoverflow
Solution 2 - DockerBrennan CheungView Answer on Stackoverflow
Solution 3 - Dockerjersey beanView Answer on Stackoverflow
Solution 4 - DockerlsimmonsView Answer on Stackoverflow
Solution 5 - DockergeckosView Answer on Stackoverflow