Skip to content

Instantly share code, notes, and snippets.

@a-pagano
Last active December 21, 2017 09:35
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 a-pagano/a965fc98faa2d3e566d68234f50ec064 to your computer and use it in GitHub Desktop.
Save a-pagano/a965fc98faa2d3e566d68234f50ec064 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Put these functions into your ~/.bashrc
# Then source ~/.bashrc
# USAGE: get-docker-container-id pattern
# DESCRIPTION: This function takes as parameter an image name (or partial image name) and prints out the associated container hash if any
# EXAMPLE: get-docker-container-id ubuntu --> c3a364dd56df
function get-docker-container-id(){
targetcontainer=$(docker ps |awk "/$1/" | tail -1)
echo -e "\nFound one container:\n\n$targetcontainer\n\n" >&2
local containerid=$(echo $targetcontainer|awk "{print \$1}" |tr -d '[:space:]')
echo "$containerid"
}
# USAGE: docker-bash pattern
# DESCRIPTION: This function takes as parameter an image name (or partial image name) open an interactive bash session inside it
# EXAMPLE: docker-bash ubuntu --> root@c3a364dd56df:/home ...
function docker-bash(){
docker exec -it $(get-docker-container-id $1) bash
}
# USAGE: docker-id pattern
# DESCRIPTION: This function takes as parameter an image name (or partial image name) and copies the associated container hash to the clipboard if any
# EXAMPLE: docker-id ubuntu --> copies "c3a364dd56df" to your clipboard
function docker-id() {
echo -n "$(get-docker-container-id $1)" |pbcopy
echo -e "Container id $containerid copied to clipboard!\n"
return 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment