Skip to content

Instantly share code, notes, and snippets.

@ilius
Last active March 7, 2024 06:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ilius/66e76cea7c28f91037bc95db7013d445 to your computer and use it in GitHub Desktop.
Save ilius/66e76cea7c28f91037bc95db7013d445 to your computer and use it in GitHub Desktop.
Docker Bash functions
function docker-running-by-name() {
# docker ps --filter name=$1 --quiet # does not always work!
docker ps | grep "$@" | sed 's/ .*//' | head -n1
}
function docker-shell-to-name() {
ID=$(docker-running-by-name $1)
if [ -z $ID ] ; then
echo "No running container by image name $1"
return
fi
shift
if [ -z $1 ] ; then
docker exec -it $ID /bin/bash
return
fi
docker exec -it $ID "$@"
}
function docker-logs-by-name() {
ID=$(docker-running-by-name $1)
if [ -z $ID ] ; then
echo "No running container by image name $1"
return
fi
docker logs "$ID" --follow
}
function docker-ps-by-name() {
docker ps --filter name=$1
}
function docker-gateway-ip() {
ID=$(docker-running-by-name $1)
if [ -z $ID ] ; then
echo "No running container by image name $1"
return
fi
docker inspect -f '{{range.NetworkSettings.Networks}}{{.Gateway}}{{end}}' $ID
}
function docker-ip() {
ID=$(docker-running-by-name $1)
if [ -z $ID ] ; then
echo "No running container by image name $1"
return
fi
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $ID
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment