Often we need to run shell commands on a running docker container. The traditional way would be to run docker exec -it <container> /bin/bash
for example. Below is an alias to simplify this process a bit.
# Run a shell in the specified container.
dkexsh() {
if [ $# -ne 1 ]; then
echo "Usage: $funcstack[1] CONTAINER_ID"
return 1
fi
if docker exec -it $1 /bin/bash; then :; else
echo "/bin/bash not available in container. Trying /bin/sh"
docker exec -it $1 /bin/sh
fi
}
alias dksh='dkexsh'
Now we can run dksh <container>
and will be dropped into a shell on that container. You can modify it to fruther add different shells, but the alias will first try to execute /bin/bash
and if that fails /bin/sh
.