Skip to main content

Docker CLI Tips and Shortcuts

1. Using Command Aliases

Alias Full Command Description
docker alias alias d='docker' Shorten the docker command to 'd'
docker ps alias alias dps='docker ps' List running containers
docker images alias alias di='docker images' List Docker images
docker logs alias alias dl='docker logs' Show container logs
docker exec alias alias de='docker exec' Execute a command in a running container
docker build alias alias db='docker build' Build an image from a Dockerfile
docker run alias alias dr='docker run' Run a command in a new container
docker stop alias alias ds='docker stop' Stop one or more running containers
docker rm alias alias drm='docker rm' Remove one or more containers
docker rmi alias alias drmi='docker rmi' Remove one or more images

Example: Setting up aliases in .bashrc or .zshrc

# Add to ~/.bashrc or ~/.zshrc
alias d='docker'
alias dps='docker ps'
alias di='docker images'
alias dl='docker logs'
alias de='docker exec'
alias db='docker build'
alias dr='docker run'
alias ds='docker stop'
alias drm='docker rm'
alias drmi='docker rmi'

2. Formatting Output

Command Description Example
docker ps --format Format the output of docker ps using Go template syntax docker ps --format "table {{.Names}}\t{{.Image}}\t{{.Status}}"
docker images --format Format the output of docker images using Go template syntax docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}"
docker inspect --format Format the output of docker inspect using Go template syntax docker inspect --format "{{.Config.Entrypoint}}" container_name

Example: Formatting Docker Container List

docker ps --format "table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}"

Example: Custom JSON Output

docker ps --format "{{json .}}"

3. Using Filters Effectively

Filter Description Example
name Filter by container/image name --filter name=nginx
status Filter by container status (running, paused, exited) --filter status=running
label Filter by label key=value --filter label=environment=prod
before Filter containers created before a specific container ID or name --filter before=webapp
since Filter containers created after a specific container ID or name --filter since=database
ancestor Filter by image ancestor (image or image ID) --filter ancestor=alpine
network Filter by network name or ID --filter network=bridge
expose Filter by exposed port (published or private) --filter expose=80

Example: Filtering Containers by Label

docker ps --filter label=environment=prod --filter label=team=backend

Example: Filtering Images by Parent Image

docker images --filter ancestor=ubuntu:20.04
Note: Filters can be combined in a single command. Use --filter multiple times to apply multiple filters.

4. Using Quiet Mode

Quiet mode in Docker CLI suppresses output and only shows the essential information, which is useful for scripting or when you need minimal output.

Flag Description Usage Example
-q Suppresses output and only shows the container ID or image ID docker ps -q
--quiet Same as -q, alternative long form docker images --quiet

Example: Using quiet mode to get container IDs

docker ps -q

Output:

abc123def456
def456ghi789

Example: Using quiet mode to get image IDs

docker images --quiet

Output:

sha256:abc123def456...
sha256:def456ghi789...

5. Combining Commands with Pipes

Command Description Example
docker ps List running containers docker ps
docker ps -a List all containers (running and stopped) docker ps -a
docker images List Docker images docker images

Example: Filter containers by status using grep

docker ps -a | grep "Exited"

Example: Get container IDs of stopped containers

docker ps -a --format "{{.ID}}" | grep "^<container_id_prefix>"

Example: Count running containers

docker ps | wc -l
Note: The docker ps command can be combined with other Unix tools like grep, awk, and cut to filter and process container information efficiently.

6. Leveraging Bash Completion

Docker CLI supports bash completion to speed up command typing and reduce errors. This feature provides auto-completion for commands, options, container names, image names, and more.

Feature Description Installation
Bash Completion Enables tab completion for Docker CLI commands in bash shells. Run docker completion bash to generate the completion script and source it in your shell.

Example: Enabling Bash Completion

# Generate completion script
docker completion bash &gt; ~/.docker-completion

# Source the completion in your shell
source ~/.docker-completion

# Or add to .bashrc for persistence
echo "source ~/.docker-completion" &gt;&gt; ~/.bashrc
Note: Bash completion requires Docker CLI version 20.10 or higher.

Benefits of Bash Completion

  • Reduces typing errors
  • Speeds up command execution
  • Improves discoverability of Docker commands and options

7. Using Latest Container

Command Description Example
docker run --rm Run a container and automatically remove it when it exits docker run --rm nginx
docker run -it Run container interactively with a TTY docker run -it ubuntu /bin/bash
docker exec -it Execute a command in a running container docker exec -it my_container /bin/sh
docker ps List running containers docker ps
docker ps -a List all containers (running and stopped) docker ps -a
docker logs View logs from a container docker logs my_container
docker stop Stop a running container docker stop my_container
docker start Start a stopped container docker start my_container
docker kill Forcefully stop a container docker kill my_container
docker inspect Get detailed information about a container or image docker inspect my_container

Example: Run a container with custom port mapping

docker run -d --name web_server -p 8080:80 nginx

Example: Run a container with environment variables

docker run -d --name app_server -e NODE_ENV=production node:18

8. Chaining Docker Commands

Example: Run and Remove Container in One Command

docker run --rm nginx:latest
Command Description Notes
docker run --rm Run container and automatically remove it when it exits Useful for one-time tasks or testing
docker run --rm -v /host/path:/container/path Mount volume and remove container after use Great for temporary file operations
docker run --rm -p 8080:80 Map port and remove container after use Useful for testing services without leaving containers behind
Note: The --rm flag is only effective when the container exits cleanly. If a container is stopped forcefully (e.g., docker stop with timeout), it may not be removed.

Example: Chain Multiple Commands with &&

docker build -t myapp . && docker run --rm -p 8080:80 myapp

Example: Use Shell Pipes to Chain Commands

docker ps -aq | xargs docker rm

Best Practices for Chaining Commands

  • Use --rm to clean up containers after use
  • Combine commands with && to ensure success before proceeding
  • Use shell pipes for bulk operations (e.g., removing all stopped containers)

9. Using Shell Variables in Commands

Variable Usage Description
DOCKER_HOST export DOCKER_HOST=ssh://user@host Specifies the Docker daemon host to connect to
DOCKER_TLS_VERIFY export DOCKER_TLS_VERIFY=1 Enables TLS verification for Docker daemon connections
DOCKER_CERT_PATH export DOCKER_CERT_PATH=/path/to/certs Path to the directory containing Docker TLS certificates

Example: Setting and Using Variables

export DOCKER_HOST=ssh://user@remote-host
export DOCKER_TLS_VERIFY=1
docker ps
Note: Shell variables can be used to configure Docker CLI behavior, such as connecting to remote hosts or enabling TLS verification.

10. Exporting Environment Variables

Variable Value Description
DOCKER_HOST unix:///var/run/docker.sock Specifies the Docker daemon socket to connect to
DOCKER_TLS_VERIFY 1 Enables TLS verification for secure connections
DOCKER_CERT_PATH /home/user/.docker/certs Path to the directory containing TLS certificates

Example: Setting Environment Variables

export DOCKER_HOST=unix:///var/run/docker.sock
export DOCKER_TLS_VERIFY=1
export DOCKER_CERT_PATH=/home/user/.docker/certs
Note: These environment variables are used by the Docker CLI to communicate with the Docker daemon and manage secure connections.

11. Using Docker Context Efficiently

Command Description Notes
docker context ls List all contexts Shows name, description, and default status
docker context use <context> Switch to a specific context Changes the default Docker host for commands
docker context create <name> <options> Create a new context Can specify remote host or Docker Engine endpoint
docker context inspect <context> Inspect a context Shows detailed configuration of the context
docker context rm <context> Delete a context Removes the context from the local configuration

Example: Creating a remote context

docker context create --description "Production server" \
  --docker "host=tcp://192.168.1.10:2376,ca-file=/path/to/ca.pem,cert-file=/path/to/cert.pem,key-file=/path/to/key.pem" \
  prod-server
Note: Contexts are stored in $HOME/.docker/contexts/meta.
Warning: Ensure TLS certificates are valid when connecting to remote Docker daemons.

12. Cleaning Up All Resources

Example: Remove all stopped containers, unused networks, unused images, and build cache

docker system prune -a
Option Values Description
-a --all Remove all unused images not just dangling ones
--force -f Do not prompt for confirmation
--volumes true Remove named volumes declared as dangling
Note: Use docker system prune -a to clean up all unused resources including containers, networks, images, and build cache. This command is safe to run regularly but will remove all unused resources.
Warning: This command will remove all stopped containers, unused networks, and unused images. Ensure you have backed up any important data before running this command.

Was this article helpful?