Last active
September 12, 2024 21:13
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.