Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ctrl-p and Ctrl-n behaving unexpectedly under Docker

Tags:

linux

bash

docker

For the life of me I can't seem to figure out why ctrl-p and ctrl-n don't work like they're supposed to under the Docker images I have been working with. ctrl-p should work just like the up arrow but I usually have to press it twice to get the last command I ran. And it cycles through the history in what seems to be a random fashion.

Maybe someone can help me make some sense of this.

docker run -it buildpack-deps:trusty # run a Linux based image

root@74cbcf321fae:/# ls
bin  boot  dev  etc  home  lib  lib64  ...
root@74cbcf321fae:/# touch hello

If I press up here, it should show the touch command, followed by ls. If I press Ctrl-p however, nothing comes up the fist time. When I press it again, ls appears magically.

Can someone help me make sense of these. I can't live without Ctrl-p and Ctrl-n.

like image 675
jaywhy13 Avatar asked Jan 24 '17 04:01

jaywhy13


People also ask

What happens when you press Ctrl-P Q inside the container in docker?

If the container was run with -i and -t , you can detach from a container and leave it running using the CTRL-p CTRL-q key sequence. Note: A process running as PID 1 inside a container is treated specially by Linux: it ignores any signal with the default action.

How do I exit interactive mode in docker?

If you want to stop and exit the container, and are in an interactive, responsive shell - press ctrl+d to exit the session. You could as well type the exit command. TL;DR: press ctrl+c then ctrl+d - that means, keep the ctrl key pressed, type a c, and let go of ctrl. Then the same with ctrl and d.

How do you stop the container in detached mode?

To stop this container, press Ctrl+C in the terminal where it was started. Alternatively, you may start a container in detached mode using the -d option. To see all running containers, use the docker ps command.

How do I stop docker Ctrl C?

Note that pressing `Ctrl+C` when the terminal is attached to a container output causes the container to shut down. Use `Ctrl+PQ` in order to detach the terminal from container output. For more details, see the official docker documentation.


1 Answers

It looks like this has been removed (or moved) in the Docs, but it used to live here: https://docs.docker.com/engine/reference/commandline/attach/

Edit: It looks like they reference the below in the Configuration Files documentation.

The command sequence to detach from a docker container is ctrl-p ctrl-q, which is why ctrl-p doesn't work as expected. When you hit ctrl-p, docker is waiting on ctrl-q, so nothing happens.

You can use the new --detach-keys argument to docker run to override this sequence to be something other than ctrl-p:

docker run -ti --detach-keys="ctrl-@" ubuntu:14.04 bash

$# ls
$# <--- Ctrl-P here will display ls now
$# <--- Ctrl-@ here will detach from the running container

If you want, you can add this to your ~/.docker/config.json file to persist this change:

{
    ...
    "detachKeys": "ctrl-@",
    ...
}

More details on this can be found here: https://github.com/docker/docker/pull/15666 as I can't find it in the docs anymore.

like image 143
Chris McKinnel Avatar answered Oct 17 '22 15:10

Chris McKinnel