Skip to content

Instantly share code, notes, and snippets.

@Hammer2900
Forked from danfergo/docker_exec_alias.md
Created February 25, 2019 14:05
Show Gist options
  • Save Hammer2900/908bf2d45af77987d5420919ba064af2 to your computer and use it in GitHub Desktop.
Save Hammer2900/908bf2d45af77987d5420919ba064af2 to your computer and use it in GitHub Desktop.
Aliasing docker exec -it bash

How to alias docker exec (with autocompletion)

Aliasing docker exec -it some_container_name bash to d some_container_name.

Step 1.1 Add to your .bashrc

d(){
    docker exec -it $1 bash
}

Step 1.2 Then source it

source ~/.bashrc

Step 2.1 For autocompletion, create /etc/bash_completion.d/d and copy to the following

_d()
{
  _script_commands=$(docker ps | sed '1d' | awk '{print $NF}')

  local cur
  COMPREPLY=()
  cur="${COMP_WORDS[COMP_CWORD]}"
  COMPREPLY=( $(compgen -W "${_script_commands}" -- ${cur}) )

  return 0
}
complete -o nospace -F _d d

Step 2.2 Source it

source /etc/bash_completion.d/d

Step 3 🎉, Bash into containers using the alias, and complete the container with tab.

d some_container_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment