Docker attach to running container
Attach to the running Docker container interactively using FZF.
Install FZF: brew install fzf
Add the following function to the .bashrc or .zshrc
1dex() {2 CONTAINER=$(docker ps | grep -v CONTAINER | awk '-F ' ' {print $NF}' | fzf)3 if [ ! -z "$CONTAINER" ]4 then5 docker exec -it "$CONTAINER" bash6 fi7}Let's break it down:
docker psLists all running Docker containersgrep -v CONTAINERFilter out headers fromdocker psoutputawk '-F ' ' {print $NF}'Extracts only container namesfzfSelect a container using FZFif [ ! -z "$CONTAINER" ]Checking if container was selecteddocker exec -it "$CONTAINER" bash- opens an interactive shell in the selected container.
More useful functions in my dotfiles repo
Interactively attach to running Docker container using FZF