docker change Ctrl+p to something else?

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 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).


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!


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


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.


To use this without changing global configuration

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