Skip to Content
All posts

Useful aliases for docker

 — #docker#devops

Table of contents

🢒Click to expand!

Useful aliases for docker

Overview

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"

Get container process

alias dps="docker ps"

Get process included stop container

alias dpa="docker ps -a"

Get images

alias di="docker images"

Get container IP

alias dip="docker inspect --format '{{ .NetworkSettings.IPAddress }}'"

Run daemonized container

alias dkd="docker run -d -P"

Run interactive container

alias dki="docker run -i -t -P"

Execute interactive container

alias dex="docker exec -i -t"

Stop all containers

alias dstop='docker stop $(docker ps -a -q)'

Remove all containers

alias drm='docker rm $(docker ps -a -q)'

Stop and Remove all containers

alias drmf='docker stop $(docker ps -a -q) && docker rm $(docker ps -a -q)'

Remove all images

alias dri='docker rmi $(docker images -q)'

Dockerfile build

alias dbu='docker build -t=$1 .'
dalias() { alias | grep 'docker' | sed "s/^\([^=]*\)=\(.*\)/\1 => \2/" | sed "s/['|\']//g" | sort; }

Bash into running container

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

Conclusion

Using aliases for Docker commands can significantly streamline container management by reducing the need to repeatedly type out long or complex commands. Aliases allow developers to customize and simplify their workflows, enabling quicker execution of common tasks, such as building images, running containers, or managing Docker networks. By creating personalized shortcuts, developers can work more efficiently and avoid errors caused by mistyping lengthy commands, making Docker usage more intuitive and accessible.