2

Is there a way to have docker compose run docker-compose in a bash alias?

I know docker is a command of it's own so I don't think you can, I thought I would ask since it drive's me nuts making aliases when I type docker so much.

i already have alias dco=docker-compose but you know, a brother is looking for a better way. :)

5
  • Why not make your own docker command that calls the real docker or docker-compose based on the args? Commented Nov 9, 2017 at 15:36
  • 1
    Alias are just straight-up text replacement; alias dco='docker compose' would work. Commented Nov 9, 2017 at 15:40
  • Or are you looking for a way to parameterize the alias, so that one alias covers multiple subcommands? For that, you need to use a function: something like d () { if [[ $1 = com* ]]; then docker compose "${@:2}"; elif ... fi; }, which would let you run d co to run docker compose. Commented Nov 9, 2017 at 15:42
  • 1
    See similar question: stackoverflow.com/questions/34748747/… Commented Nov 9, 2017 at 16:14
  • @chepner and if we want to bash autocomplete work with dco like with docker-compose, we can add little workaround. First step, find complete config string with complete | grep docker-compose. Second, change found string from complete -F _docker_compose docker-compose to complete -F _docker_compose dco and save it to the file ~/.bash_completion. Now I got any subcommand expand with <Tab> :) Commented Dec 12, 2017 at 22:07

1 Answer 1

4

You could make a function instead:

docker() {
  if [ "$1" = "compose" ]
  then
    shift
    docker-compose "$@"
  else
    command docker "$@"
  fi
}

Here the command prevents the docker function from calling itself again.

See also: Can I alias a subcommand? (shortening the output of `docker ps`) as commented by @zlemini

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.