Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker attach: Why can't I detach from my Docker container?

For some reason, I can't detach from Docker containers after running docker attach <container>. The documentation says to use Ctrl-p, Ctrl-q, but that doesn't seem to work. I've also tried ctrl-q + ctrl-p (combination, as opposed to in-sequence) and ctrl-shift-q, ctrl-shift-p and ctrl-shift-q + ctrl-shift-p. Even setting a detach key, for example --detach-keys="p" won't detach from the container.

Furthermore, other things don't seem to be working. For example, according to documentation, ctrl-c should cause the attached container to stop and detach. However, I haven't been able to get ctrl-c working on any of my containers. Unlike ctrl-q, however, ctrl-c renders feedback as expected, showing a ^C in the terminal.

I've noticed that for some reason, when I press ctrl-p, I get a ^P in the terminal, but pressing ctrl-q or ctrl-shift-q renders no terminal feedback.

Can anyone venture a guess as to why this might be happening?

I'm using iTerm2 on MacOS if it matters. Also, the containers in question were launched with docker-compose.

Edit: For clarity, I launched my container with docker-compose up on the following compose file:

version: '3'

services:
  test:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: container-test
    ports:
      - "5050:5050"

Then I attach using:

$ docker attach container-test

Edit 2: After some testing, I can confirm this issue still exists in MacOS Terminal.

like image 405
Kirk Sefchik Avatar asked Feb 23 '18 19:02

Kirk Sefchik


2 Answers

You can use --sig-proxy=false to prevent signals being passed to container and detach using Ctrl+C:

docker attach --sig-proxy=false container-test

This seems to be a known issue: https://github.com/docker/for-mac/issues/1598

like image 116
Devstr Avatar answered Oct 31 '22 17:10

Devstr


I found that by adding the following lines to my docker-compose, I could get it to exit and respond to inputs.

services:
  test:
    // etc...
    stdin_open: true
    tty: true
    // etc...
like image 3
Kirk Sefchik Avatar answered Oct 31 '22 17:10

Kirk Sefchik