-
Notifications
You must be signed in to change notification settings - Fork 18.7k
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
Cannot kill or detach a docker container using Ctrl-C or Ctrl-\ #2838
Comments
Starting with docker 0.6.5, you can add If you use Test 1:
The container is still listed. Test 2:
the container is not there (it has been terminated). If you type A pull request to fix the docs for the Hello World daemon sample is here: I'm unaware of where you saw the recommendation to You might also find this mailing list thread helpful. |
@lhazlewood Well, -t -d lets you kill the container with ctrl-c in the docker attach, however the problem is:
In this case ctrl-C does nothing. I expected it to kill the "docker attach" process (not the container). |
If the daemon was not started with -t, i think docker attach should default to -sig-proxy=false |
From what I can gather you need both |
@vmalloc That depends on what you expect. I expect to be able to detach from "docker attach" with ctrl-c, not kill the daemon. That is not currently possible without manually specifying -sig-proxy=false. If you accidentally do this the only way out is to kill the docker attach process from another terminal. |
Ah, sorry I was a bit confused above. If you run "docker run -d -t" and the docker attach, then ctrl-c does detach, not kill the daemon. The problem is that if you forgot the -t, and then ever use docker attach you end up with something you can't detach without killing it from a different terminal. |
@alexlarsson Yes, the two test cases I show above show what happens w/ and w/o |
Yes, I am experiencing the same issue. An easy way to reproduce is |
Also see #2855 |
To have ctrl+c stop the container you must use -it Closing. |
Why was this closed? Can you please explain the reason? I asked because it is obvious that the majority of the community does not want the current default behavior. Also note that ctrl+pq does not work on Mac OS X running Ubuntu in VirtualBox. |
@lhazlewood Because the issue wasn't about ctrl+pq it was about getting out of attach with ctrl-c, which is not supported since "attach" is litterally attaching you to the running process and should be expected behavior. If we want to discuss changing ctrl-pq, and I know I've seen other issues around it that's fine. And I write all this in the friendliest of tone, I'm terse by nature, sorry about that. |
I am VERY confused by all this. The default behaviour when being in a shell prompt is that ^C kills the running foreground process. And if no such process exists, it just do nothing, except maybe show a new prompt. ^D on the other hand do exit the shell, without stopping the machine. This is what I expect, because this is what everyone else do. So, why do Docker do it differently? And why do I read this strange discussion? Am I missing something? |
@rolkar Because there are two foreground processes in this case. I personally think this should behave in much the same way as SSH, though with a different escape sequence. |
Still don't get it. Why Ctrl-C doesn't send SIGINT? |
@Vanuan - because the process is running in a docker daemon and you only attach to that daemon's tty. Lets say that ^C would kill the process that the daemon has started. Then you would kill the container daemon. Not unreasonable. Maybe what you and I want (in most cases). But, it is not obviously the right thing to do. And some have other opinions. |
Yeah, I figured it out. Docker actually sends SIGINT, but the kernel ignores it because process id is 1. And since the process doesn't have its own SIGINT handler, nothing happens. |
The kernel doesn't ignore it, the process does. |
@cpuguy83 |
@Vanuan The signal is always sent to the process you specified, but some processes don't load signal handlers when they are pid 1, so the signal gets ignored. |
Doesn't appear to be true: // main.c
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
void catch_interrupt(int sig) {
printf("Interrupting...\n");
}
int main() {
signal(SIGINT, catch_interrupt);
sleep(10);
return 0;
}
Same setup with
As you see, signal handler is loaded. And even though PID remains to be "1", it responds to Ctrl-C and terminates (though we didn't ask it to terminate, we only handled it). But when there's no signal handler registered, it doesn't respond to Ctrl-C and nothing happens. |
@Vanuan Yes, this is exactly as I said. |
I referred to this part:
Does it mean something else? Signal handlers are always loaded when they're registered. But it appears that there are some default signal handlers which are not loaded if pid = 1 AND signal handler is not registered. Is that what you meant? |
@Vanuan exactly. |
I'm still confused about this. Example run:
So ctrl+c doesn't send SIGINT to either the docker process or the ping process. Is |
@johshoff It depends on how |
Thanks, @cpuguy83, that makes it clearer. |
In summary:
@guiambros that is so useful it's going in my aliases. |
Also for swarm mode: The corresponding compose change was recently merged, so in the next version you'd be able to do this:
Finally you won't have to modify images just to add |
Just adding this as additional information (some of which is mentioned in earlier comments); While using Tini provides the following features;
If you know that the process running inside the container is a "bad actor", and doesn't handle reaping, using If you're using For example; the following CMD /usr/sbin/mysqld Because of this, To make the container's process run without a shell, use the JSON ("exec" form). For example, below is the CMD ["mysqld"] But what if you need to run some commands when starting the container (before running the container's main process), such as setting permissions, or doing some setup the first time the container is run? These steps can be done in an entrypoint-script. The entrypoint script can run a shell, perform initialization (see for example the init steps in the official WordPress entrypoint script), and at the end switch to the container's main process using When using exec "$@" The Dockerfile uses both an ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["apache2-foreground"]
TL;DR; the |
Update: This has been answered here #37200 @rayfoss @thaJeztah Again I would like |
Update: This has been answered here #37200 @tsl0922 Thank you but that's what I'm trying to avoid doing. I feel that I might be doing a command called |
TTY has nothing to do with being able to send a signal or catch it. Please read my messages in that issue thread you mentioned. |
One question: wonder what's the solution used by official images...say nginx. Nginx container can be terminated using SIGINT when not in detach mode. |
@bidiu First of all, it uses exec form: CMD ["nginx", "-g", "daemon off;"] Secondly, nginx handles signals by its own. |
You could figure out it yourself by looking at its Dockerfile: |
@Vanuan |
So there's shell form and exec form. Shell form is used like this:
Exec form like this:
In first case the shell handles your signal, in the second case the nginx process |
For folks coming here after not being able to stop a |
thanks to moby/moby#2838
Without this commit, you have to provide the --init argument to Docker. See moby/moby#2838 (comment) This also adds a test that verifies that this works.
As of right now, with Windows 10, you need to run like this inside of git bash
|
if /bin/sh doesn't ignore Ctrl-C, why it seems |
Without `-it` ctrl+c won't work, TESTED BY ME ON windows. Issue resolved at link - moby/moby#2838 (comment)
This fixed my issue with Thanks @FossPrime ! |
can you tell
ITS WORKING FOR ME ON MAC |
Hi,
Running docker 0.6.7 on Ubuntu 13.04.
I built a container whose CMD executes a uwsgi process (also tried with other executables). I run without detaching, and sigproxy is True.
Hitting Ctrl-C seems to have no effect over the running container and the only way to leave it or kill it is with
docker kill
anddocker stop
...Is this a known issue?
The text was updated successfully, but these errors were encountered: