Skip to content

Instantly share code, notes, and snippets.

@jaamo
Created April 26, 2016 07:18
Show Gist options
  • Save jaamo/15eacfa32c8051b0e9567672799c1b65 to your computer and use it in GitHub Desktop.
Save jaamo/15eacfa32c8051b0e9567672799c1b65 to your computer and use it in GitHub Desktop.
Shortcut to execute docker exec -it ID bash
#!/bin/bash
# Print out a list of docker containers.
i=0
firstrun=0
while read x
do
if [ $firstrun == 0 ]
then
echo "$x"
else
echo "[$i] $x"
i=$((i+1))
fi
firstrun=1
done << EOF
$(docker ps)
EOF
# Ask for container index
read container_index
# Get requested container id
i=0
container_id=""
for x in `docker ps -q`
do
if [ $container_index == $i ]
then
container_id=$x
fi
i=$(($i+1))
done
# Run bash.
docker exec -it $container_id bash
@henrikuittinen
Copy link

henrikuittinen commented Apr 26, 2016

If you change the last line to this, it would fall back to shell if bash is not available (some containers don't have bash)
docker exec -it $container_id sh -c '/bin/bash || /bin/sh'

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