DEV Community

Cover image for Useful aliases for docker
Rubin
Rubin

Posted on

Useful aliases for docker

Docker has been there for a long time and its my top most used tool whether for spinning up a web server or trying out a new tool.
If you are like me and use docker on your day to day dev workflow, these aliases would help you save few keystrokes for common use cases and save your time.
You can set up in your shell configuration file (like .bashrc, .zshrc, etc.):

Get latest container ID

alias dl="docker ps -l -q"
Enter fullscreen mode Exit fullscreen mode

Get container process

alias dps="docker ps"
Enter fullscreen mode Exit fullscreen mode

Get process included stop container

alias dpa="docker ps -a"
Enter fullscreen mode Exit fullscreen mode

Get images

alias di="docker images"
Enter fullscreen mode Exit fullscreen mode

Get container IP

alias dip="docker inspect --format '{{ .NetworkSettings.IPAddress }}'"
Enter fullscreen mode Exit fullscreen mode

Run daemonized container, e.g., $dkd base /bin/echo hello

alias dkd="docker run -d -P"
Enter fullscreen mode Exit fullscreen mode

Run interactive container, e.g., $dki base /bin/bash

alias dki="docker run -i -t -P"
Enter fullscreen mode Exit fullscreen mode

Execute interactive container, e.g., $dex base /bin/bash

alias dex="docker exec -i -t"
Enter fullscreen mode Exit fullscreen mode

Stop all containers

alias dstop='docker stop $(docker ps -a -q)'
Enter fullscreen mode Exit fullscreen mode

Remove all containers

alias drm='docker rm $(docker ps -a -q)'
Enter fullscreen mode Exit fullscreen mode

Stop and Remove all containers

alias drmf='docker stop $(docker ps -a -q) && docker rm $(docker ps -a -q)'
Enter fullscreen mode Exit fullscreen mode

Remove all images

alias dri='docker rmi $(docker images -q)'
Enter fullscreen mode Exit fullscreen mode

Dockerfile build, e.g., $dbu tcnksm/test

alias dbu='docker build -t=$1 .'
Enter fullscreen mode Exit fullscreen mode

Show all alias related docker

dalias() { alias | grep 'docker' | sed "s/^\([^=]*\)=\(.*\)/\1 => \2/" | sed "s/['|\']//g" | sort; }
Enter fullscreen mode Exit fullscreen mode

Bash into running container

alias dbash='docker exec -it $(docker ps -aqf "name=$1") bash'
Enter fullscreen mode Exit fullscreen mode

Top comments (0)