Skip to content

Instantly share code, notes, and snippets.

@tmuth
Last active September 12, 2024 21:13
Show Gist options
  • Save tmuth/29533265de89cc65124fd215c8ff9494 to your computer and use it in GitHub Desktop.
Save tmuth/29533265de89cc65124fd215c8ff9494 to your computer and use it in GitHub Desktop.
"Docker exec -it $1 bash" bash function for attaching to a running container
function docker_exec {
name="${1?needs one argument}"
containerId=$(docker ps | awk -v app="$name" '$2 ~ app{print $1}')
if [[ -n "$containerId" ]]; then
docker exec -it $containerId bash
else
echo "No docker container with name: $name is running"
fi
}
alias dx='docker_exec $1'
@tmuth
Copy link
Author

tmuth commented Aug 14, 2017

If you're constantly attaching to a running docker image, this makes it much easier. The original code was posted here: https://stackoverflow.com/questions/42422117/command-line-shortcut-to-connect-to-a-docker-container by anubhava. I just made it a function and made a small change or two.

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