fkr.dev

About

Hio, this is Flo. Born 1990, I like doing things and this is my blog. Into lisps, oil painting and esoteric text editors. On the interwebs I like to appear as a badger interested in cryptography. Every other decade I like spinning old jazz/funk records or programming experimental electronic music as Winfried Scratchmann. Currently located in Aix la Chapelle.

Interactive detach from a running docker compose service without termination

Short one.

As you may know, a common annoyance for docker compose users is the default behavior of the docker compose up command: it ensures a (set of) service(es) defined in a configured/accessible docker-compose.yml are up and running. It also attaches to the services stdout log following updates, i.e. remaining in foreground until the services are shutdown/terminate. Upon pressing Ctrl-c to cancel the live log view, not only the log viewing, but also the full set of services is shut down, which is usually not the desired behavior.

Along a set of shortcuts simplifying docker compose operation I therefore also define an alias that allows to detach from the running log-view-process without terminating the services:

alias docker-detach='pgrep -lfa --euid "$USER" docker | grep "/usr" | grep -v "grep" | fzf | cut -d" " -f1 | xargs -i kill -9 {}'

As usual, the alias is probably not ideal but good enough. It allows interactive selection among the running attachments using input filter fzf.

Upon selection of the running command, we extract its pid and pass it to kill -9, causing it to terminate while keeping the actual service alive. The alias may be added to your preferred shells rc file to be set up on login/shell start. (~/.zshrc, ~/.bashrc, ..)