Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Paste or link these into your ~/.bash_profile
######################################################
# Docker Shortcuts
# Maintainer geoffrey.hoffman@gmail.com
######################################################
# Load these into your bash profile, e.g. add
# source ~/.docker_shortcuts.sh
# to ~/.bash_profile
echo "docker shortcuts loaded"
dls() {
docker ps
}
dll() {
docker ps -a
}
dim() {
docker images
}
dri() {
if [[ $# == 0 ]]
then
echo
echo "Remove which image? Provide 1 argument. Here are the images you have..."
echo
dim
echo
return 0
fi
docker rmi $1
}
# docker stop
dsto() {
if [[ $# == 0 ]]
then
echo "Which container? Provide 1 argument."
return 0
fi
docker stop $1
}
# docker start
dsta() {
if [[ $# == 0 ]]
then
echo "Which container? Provide 1 argument."
return 0
fi
docker start $1
}
# docker remove
drm() {
if [[ $# == 0 ]]
then
echo "Which container? Provide 1 argument."
return 0
fi
docker rm $1
}
# docker stop all running
dsar() {
if [[ $# == 0 ]]
then
docker ps | tail -n +2 |cut -d ' ' -f 1 | xargs docker stop
else
dsto $1;
fi
}
alias dstop='dsar'
# docker remove all stopped
dras() {
docker ps -a | tail -n +2 | grep Exited | cut -d ' ' -f 1 | xargs docker rm
}
# docker kill all
docker-killall() {
dsar
dras
}
alias dka='docker-killall'
# Docker get an ext ip
dip() {
if [[ $# == 0 ]]
then
echo "Which container? Provide 1 argument."
return 0
fi
docker inspect --format='{{.NetworkSettings.IPAddress}}' $1
}
drun() {
if [[ $# == 0 ]]
then
echo "Which container? Provide 1 argument."
return 0
fi
case "$1" in
"redis" )
docker run -d --name redis -e APP=redis -p 6379:6379 redis:latest;;
"db" )
docker run -d --name db --volumes-from mysql_data -e APP=db -e MYSQL_ROOT_PASSWORD=REDACTED -p 3306:3306 db:latest;;
"app" )
docker run -d --name app -e APP=app --link redis:redis --link db:db -v ~/Projects:/Projects app:latest;;
"web" )
docker run -d --name web -e APP=web --link app:app --volumes-from app -p 80:80 -p 443:443 web:latest;;
esac
}
docker-enter() {
if [[ $# == 0 ]]
then
echo "Which container? Provide 1 argument."
return 0
fi
docker exec -ti -e "COLUMNS=200 LINES=51" $1 bash
}
alias de='docker-enter'
docker-mysql() {
docker exec -it db sh -c 'exec mysql -uroot -p"P455w0rd"'
}
alias dmysql='docker-mysql'
# Soft Start Docker app
function docker-up()
{
denv up
}
alias dup='docker-up'
function docker-down()
{
denv down
}
alias ddn='docker-down'
function docker-reset()
{
docker-down
docker-up
}
alias dre='docker-reset'
function denv ()
{
if [[ $# == 0 ]]
then
echo "Do you want to bring the Docker environment up or down?"
return 0
fi
if [[ "$1" == "up" ]]
then
echo "Bringing Up Docker Environment..."
cd ~/Repositories/Docker
docker-compose up -d
cd -
echo "Done."
fi
if [[ "$1" == "down" ]] || [[ "$1" == "dn" ]]
then
echo "Taking Down Docker Environment..."
cd ~/Repositories/Docker
docker-compose down
cd -
echo "Done."
fi
if [[ "$1" == "reset" ]] || [[ "$1" == "reload" ]]
then
denv down
denv up
fi
}
### Images
# remove all images
function docker_rmi_all(){
read -p "Are you sure you want to blow away your docker environment? <y/N> " prompt
if [[ $prompt == "y" || $prompt == "Y" || $prompt == "yes" || $prompt == "Yes" ]]
then
docker rmi -f $(docker images -q)
else
exit 0
fi
}
alias drmi='docker_rmi_all'
# remove untagged images
function di_rmi_untagged()
{
docker rmi $(docker images -q --filter "dangling=true") --force
}
alias dirmi-untag='di_rmi-untagged'
# list all images
function docker_image_list()
{
docker images
}
alias dii='docker_image_list'
function docker_remove_all_none_images()
{
docker images | grep '<none>' | awk -F' ' '{print $3}' | xargs docker rmi
}
alias drmian='docker_remove_all_none_images'
### Containers
# wipe all containers
function docker_container_remove_all()
{
docker rm -f $(docker ps -a -q)
}
alias dcrma='docker_container_remove_all'
# remove all stopped containers
function docker_remove_stopped(){
docker rm $(docker ps -a | grep Exited | awk '{print $1}')
}
alias dcrms='docker_remove_stopped'
# start rx env docker containers
function rx_docker_compose_up(){
cd ~/Repositories/Docker
docker-compose up -d
}
alias rxenvup='rx_docker_compose_up'
# stop rx env docker containers
function rx_docker_compose_down(){
cd ~/Repositories/Docker
docker-compose down
}
alias rxenvdown='rx_docker_compose_down'
function docker_xdebug(){
sudo ifconfig lo0 alias 10.254.254.254
}
alias doxd='docker_xdebug'
### Volumes
function docker_volume_list(){
docker volume ls
}
alias dvl='docker_volume_list'
function docker_volume_remove(){
if [ $# -eq 0 ]; then
echo "docker_volume_remove requires 1 arg"
else
docker volume rm $1
fi
}
alias dvr='docker_volume_remove'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment