Skip to content
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

key sequence to detach from docker-compose up #4560

Closed
smoebody opened this issue Mar 2, 2017 · 83 comments
Closed

key sequence to detach from docker-compose up #4560

smoebody opened this issue Mar 2, 2017 · 83 comments

Comments

@smoebody
Copy link

smoebody commented Mar 2, 2017

this has been topic in #291, however i want to make aware of this feature request a little more .

i vote for a key-sequence that you can use to detach from docker-compose up without stopping all containers (i.e. when you forgot to pass -d). There seems to be a key-sequence for attached docker-containers (Ctrl-p Ctrl-c). It seems plausible to reuse this in docker-compose.

@keski
Copy link

keski commented Mar 7, 2017

I too would cast a vote in this direction. For example, to see that everything is starting up as expected I often do docker-compose up without detaching (rather than having to inspect all logs). Another alternative would perhaps be to have a flag that makes Ctrl-c detach?

@woto
Copy link

woto commented May 8, 2017

hi, did you find any solution?

@thiagowfx
Copy link

There seems to be a key-sequence for attached docker-containers (Ctrl-p Ctrl-c).

Actually, the key sequence is Ctrl-p Ctrl-q. Example source.

@disarticulate
Copy link

ctrl-z appears to work version 3

@AndresPineros
Copy link

Woudln't ctrl-z stop the process?

@M4rotte
Copy link

M4rotte commented Nov 18, 2017

Ctrl+z stops the docker-compose process so the terminal is no more attached to any containers but they continue to run.

Ctrl+p, Ctrl+q doesn’t work for me…

Docker version 17.09.0-ce, build afdb6d4
docker-compose version 1.17.1, build 6d101fb

@nullifiedaccount3
Copy link

@M4rotte docker-compose is weird. Thanks for explanation and tip!

@perarg
Copy link

perarg commented Jan 28, 2018

@M4rotte, Ctrl + Z puts the process to background. The process is no longer running, but is still using resources such (i.e. system memory). So i don't think it is a good idea to use ctr + z.

@thiagowfx
Copy link

thiagowfx commented Jan 28, 2018

@M4rotte I don't know why it doesn't work for you, however my linked question (to Stack Overflow) has 22 upvotes at the time of this post, while my comment here magically gained 3 downvotes. There's something clearly wrong, which is left as an exercise to the reader.

@smoebody
Copy link
Author

smoebody commented Feb 2, 2018

@thiagowfx you are talking about docker, but we here talk about docker-compose, which is lacking the feature docker natively provides.

@MetaBarj0
Copy link

MetaBarj0 commented Feb 4, 2018

Did you try to :
ctrl+z : stop the process
bg : resume the last stopped process in the background ?

I often do that when I forget to specify the '-d' switch with docker-compose up

@ghost
Copy link

ghost commented Feb 8, 2018

@MetaBarj0 and still you have docker-compose up running in background. This is a workaround rather than a fix. This thread is a feature-request, not a question on how to overcome the problem, when you forgot the -d switch. Thanks to all who pointed out how to use unix tools to gain access to the consoles without stopping all started containers. Please vote if you find a key-sequence useful although you do not need it (like you do not need a car or a computer).

@piranha79
Copy link

@useltmann

first: your right and i voted for ctrl-p, ctrl-c as suggested by @smoebody.

second: @useltmann and @smoebody don't happen to be the very same person, right? the fact that your both sharing a quite seldom name and vita is just a coincidence I suppose.

@smoebody
Copy link
Author

smoebody commented Feb 9, 2018

@piranha79 yes, you are right: identical person. One is work, the other is private.

@mduft
Copy link

mduft commented Feb 15, 2018

simply putting the process to the background is no solution as streams stay attached in this case. I have some very chatty containers, so this is an issue. +1 for a "real" detach.

@ravensorb
Copy link

Please consider adding this soon

@secobarbital
Copy link

As a workaround, what I do is create an alias so I never forget:

alias dcup="docker-compose up -d"

@mallyvai
Copy link

mallyvai commented May 11, 2018

I will cast an anti-vote here.

Key sequences often conflict with custom settings with window managers like tmux and screen (including possible custom configurations), can clash with operating system hotkeys. I believe they're an anti-pattern for interfaces, even if Docker itself natively supports it.

@sr105
Copy link

sr105 commented Jun 5, 2018

Perhaps a method to tell docker-compose to detach from another terminal?

@ravensorb
Copy link

I am a little confused :) Docker supports the functionality so this isn't adding anything new -- it is just exposing functionality that already exists in the core tooling. If there is a feeling that this isn't a good approach wouldn't it make sense to look at removing it from docker itself rather than effectively breaking existing functionality?

@oninoshiko
Copy link

Considering this has gotten 105 upvotes at the time of this writing, I think the original thread's assertion that this isn't really well desired is... no longer accurate. Not having some way to detach once I've accidentally forgotten the -d, makes it EXTREMELY hard for me to even consider using docker-compose for anything other then basic testing. Even there I'm unlikely to use it because it then doesn't match how I deal with production.

Even if I background the instance I've forgotten to do it in, and start an instance with it. when I come back to the first instance, shutting it down STILL kills all containers (even ones that it didn't start!). That makes it one of the most unforgiving commands in the UNIX world, only slightly less so then dd!

@rabbagliettiandrea
Copy link

CTRL-Z, then disown %1 to release the job

@oninoshiko
Copy link

That still leaves the local process running, just not owned by the current shell. What we are asking for is a way to detach the remote instance and completely end the local process.

@ateijelo
Copy link

Until a good solution is found for this I resorted to giving myself a safety net by adding this to my ~/.bashrc:

function docker-compose() {
    if [ "$1" == "up" -a "$#" -eq 1 ]
    then
        echo "Are you sure you want to run 'dc up' without -d?"
        echo "Run then:"
        echo "   command docker-compose up"
        return 0
    fi
    command docker-compose "$@"
}

@99Percent
Copy link

I suggest that the default to be detached mode, and add the option to run in attached, or logging to a file.

@rustyx
Copy link

rustyx commented Oct 9, 2018

The nastiest thing is - if the containers are already running happily in background, an accidental docker-compose up will attach to all of them and bring them all to foreground, so then when killed, will kill them all.

Fix this already for crying out loud.

@ateijelo
Copy link

ateijelo commented Oct 9, 2018

@rustyx I agree wholeheartedly. As someone already mentioned above, this is one of the most unforgiving mistakes one can make while using docker-compose. Heck, probably one of the most in all of the Unix world. If at least one could send a USR1 signal or something to make it detach after realizing you've made a mistake, there would be a way out.

@sr105
Copy link

sr105 commented Oct 9, 2018

Perhaps remove the attach feature completely and replace it with a printed note about running docker-compose logs -f? Or make docker-compose up fork when done to the logs -f command?

@Skydive
Copy link

Skydive commented Sep 18, 2020

@zhongjiewu - can confirm works on linux!
"Quit (core dumped)" - is this a SIGKILL?
For goodness sake - why isn't this fixed.

@friedrich
Copy link

@zhongjiewu - can confirm works on linux!
"Quit (core dumped)" - is this a SIGKILL?
For goodness sake - why isn't this fixed.

There is nothing to fix.

The SIGQUIT signal is similar to SIGINT, except that it’s controlled by a different key—the QUIT character, usually C-\—and produces a core dump when it terminates the process, just like a program error signal. You can think of this as a program error condition “detected” by the user.
https://www.gnu.org/software/libc/manual/html_node/Termination-Signals.html

@anaetrezve
Copy link

ctrl-z appears to work version 3

It's worked for me on Ubuntu 20.04

@smoebody
Copy link
Author

closing after 4 years due to wont fix

@buduboti
Copy link

Ctrl-d would be a good key combination...

@speculatrix
Copy link

Ctrl-d would be a good key combination...

not really because that's EOF and you're likely to accidentally close a shell in the target environment. CTRL-A or CTRL-B, like Screen or TMux use would be reasonable. You could have a family of commands, just as Screen or Tmux have the Ctrl-A or Ctrl-B + command key.

@reinistihovs
Copy link

why not use combination similar like "screen" does
for example ctrl+q+d

I sometimes want to run it attached for debugging purposes, and then detach later and leave it running in background.

@speculatrix
Copy link

Is there an echo here?

@buduboti
Copy link

not really because that's EOF and you're likely to accidentally close a shell in the target environment. CTRL-A or CTRL-B, like Screen or TMux use would be reasonable. You could have a family of commands, just as Screen or Tmux have the Ctrl-A or Ctrl-B + command key.

You can close a lot of programs with EOT (ex: cut) which is not closing the shell. Of course, you can close the shell with EOT too, but you can pay attention to not close it accidentally; also if you close it, you could open another terminal and continue your work after the docker-compose should run detached after EOT.

@tylerszabo
Copy link

I've found sending an explicit SIGHUP causes docker-compose to properly and gracefully terminate. It will do so without tearing down the container unless you're running with run --rm for a one-off container (as noted in #4909).

I like to avoid SIGKILL where possible and the way it's set up both SIGINT and SIGTERM will stop the container.

From an implementation end whenever the magic hotkey is decided upon having the sequence send SIGHUP to itself will keep behavior consistent with what's expected and with what you get when you just close the window.

@jivanpal
Copy link

I can confirm that @tylerszabo's suggestion of SIGHUP works on macOS, so killall -HUP docker-compose from another terminal suffices.

@rahulchandak22
Copy link

would it be possible to have docker-compose up -ad, so when you CTRL+C it won't kill them but detach at later point

@psuter80
Copy link

psuter80 commented Mar 30, 2021

Wrote a bash function to add a -d and then run logs immediately after:

@dpacmittal I am using your function, thanks for sharing. It has a little flaw though: if a container in your docker compose contains "up" in its name it will append "-d" to whatever command you execute except if your name contains "-d" in which case it will never append -d not even when the up command is used.

to fix this, simply add spaces before and after the strings you compare int he two if conditions:

  if [[ " $@ " == *" up "* ]]; then
    if [[ " $@ " == *" -d "* ]]; then

and in the countPosition function you also need to make sure that the grep statement only is true if the entire string matches:

    if echo $1 | grep -q -x "$NEEDLE"; then

i won't paste the entire script here, maybe you can just edit your command and apply this fix to keep things organized :)

@kmeaw
Copy link

kmeaw commented Jun 30, 2021

You can simply press ^Z, then SIGKILL the docker-compose process (kill -9 %1), so it will not have a chance to process SIGINT and stop all the containers.

@buduboti
Copy link

I'm using sudo docker-compose up -d && sleep 1 && sudo docker-compose logs -t -f. So I can Ctrl-c and detach from the logs while the containers are still up.

@elcintan
Copy link

elcintan commented Jan 27, 2022

For Mac:
You may run the command including &
Then, ctrl-z and bg/fg work.

@Michael1337
Copy link

I just wanted to close my session when it told me: "There are stopped jobs.", because I thought just sending them to the background with CTRL+Z would be smart.
What worked for me: Bring the process back to the foreground with fg, then SIGQUIT with CTRL+4. Then I could safely logout, no jobs active anymore and all containers still running.

@MrMino
Copy link

MrMino commented Jun 1, 2022

docker attach uses ctrl+p followed by ctrl+q. Why not just implement it the same way?

@pedz
Copy link

pedz commented Nov 23, 2022

I came here, as others, looking for a way to be able to detach from a docker compose up without killing the containers.

I have a unique solution that I thought I'd share.

First, start the containers either by doing docker compose up --detach or, as others have suggested, after the first time, docker start [container list] can be used. In both cases, the containers are running. I do a ps -ef | grep -I docker | wc -l and get 14.

Now, do the docker attach but use the --sig-proxy=false option. Without it, the ^C is caught and translated into a SIGQUIT which allows the process to shutdown gracefully which causes the containers to be killed. You don't want this. What you want is to use --sig-proxy=false and now the ^C is translated into SIGKILL. The local process is killed instantly and cleanly. Doing this, my container is still working and the total number of docker processes remains at 14 as before.

@dm17
Copy link

dm17 commented Nov 23, 2022

I can't think of a reason why up -d shouldn't be the default. Can someone remind me?

@FunctionDJ
Copy link

@dm17 i like the current attach default (if we had a detach key sequence) because detaching is something you can do instantly but starting a stack might take time.
right now attach is default. let's assume we had a detach key sequence. if you forgot the -d flag, you can instantly fix that by detaching with a key sequence.
if detach is default and you forgot to specify the attach flag, then you need to wait longer to fix that in order to get the starting logs right in your terminal because running up again takes more time.

@CanRau
Copy link

CanRau commented Mar 19, 2023

Why is this there no official solution for this?
This would be so helpful, I just added a local:init script to my package.json, so when first pulling the repo one can run this and see all the logs to ensure everything works as intended, then could detach and later just call local/local:stop

@artshade
Copy link

artshade commented Mar 20, 2023

There seems to be a key-sequence for attached docker-containers (Ctrl-p Ctrl-c).

Actually, the key sequence is Ctrl-p Ctrl-q. Example source.

Works as a charm! Thank you very much! ✨

Outputs read escape sequence and returns to the initial process/shell where docker attach "$containerName"; was.
Pressed without releasing Ctrl, though it worked with a release, too.
Started with docker compose up -d;.

It's worth to mention that the docker-compose.yaml file has the following two options set for the service:

  • tty: true # "-t";
  • stdin_open: true # "-i".

Versions:

  • Docker version 20.10.21, build baeda1f;
  • Docker Compose version v2.12.2.

OS: Linux cutehost 5.15.0-67-generic #74-Ubuntu SMP Wed Feb 22 14:14:39 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux.

@audouts
Copy link

audouts commented May 8, 2023

There really should be a way to make this happen with a control-key combination or docker-compose.

Example, docker-compose detach <container>.

@ThePrankMonkey
Copy link

Is there a historical reason that docker-compose doesn't detach by default?

I find it hard to think of a time I've ever wanted it to be attached instead of setting it to detach and then just using docker-compose logs whenever I need to see what's going on.

@audouts
Copy link

audouts commented May 10, 2023

Is there a historical reason that docker-compose doesn't detach by default?

This is a good question and it's been asked before.

It was also suggested back in 2018 that the default be detached. However, if that becomes the default, we still need a solution to detach from attached mode.

@juancampa
Copy link

For anyone still fighting with muscle memory. Here's a short addition to ~/.bashrc or ~/.zshrc that refuses to run docker-compose up without -d

docker-compose() {
  if [ "$1" = "up" ] && [ "$2" != "-d" ]; then
    echo "Refusing to run docker-compose up without -d"
  else
    /usr/bin/docker-compose "$@"
  fi
}

@artshade
Copy link

artshade commented Dec 23, 2023

For anyone still fighting with muscle memory. Here's a short addition to ~/.bashrc or ~/.zshrc that refuses to run docker-compose up without -d

docker-compose() {
# ...
    /usr/bin/docker-compose "$@"
# ...

Just in case, docker-compose is deprecated. Please consider docker compose instead.

Is it Bash? You might like a single condition then like: [[ ! "${1-}${2-}" =~ ^up\-d$ ]];.

Related:
- What is the difference between test, [ and [[ ?;
- Tests and Conditionals.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.