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 ps
Lists all running Docker containersgrep -v CONTAINER
Filter out headers fromdocker ps
outputawk '-F ' ' {print $NF}'
Extracts only container namesfzf
Select 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