Docker exec doesn't load aliases
Solution 1:
It doesn't due to limitations due to aliases being meant to be used interactively. You can probably hack around this but the easiest solution is by far to simply make your "alias" into a script and place it in /bin
.
Dockerfile
RUN echo '#! /bin/sh' >> /bin/mycommand
RUN echo 'echo "running mycommand!"' >> /bin/mycommand
RUN chmod u+x /bin/mycommand
Then it works as expected
docker exec -it f3a34 mycommand # => running mycommand!
Solution 2:
Aliases are mostly useful when you want to shorten part of a commonly used command which takes variable arguments. In a ruby dev context that might be creating an alias like
alias be="bundle exec"
because who wants to type out bundle exec
all the time?
If what you really want is a shorter version of a longer command with static arguments then you should create a script anyway. With a script, it will always be available and not be dependent on sourcing particular profiles in particular contexts (which you can basically never rely on).
A more comman case (like that above) is when you use an alias because you want to effortless string arguments on to the end of the alias. For instance
$ be rails server
or
$ be rake db:migrate
In both cases, i don't want to have to type out more than I need to. With just a dash of bash, however, you can achieve the same thing in a more versatile solution.
Similar to some of the above answers, create a file - /usr/local/bin/be
in this example. This assumes that /usr/local/bin
is included in your PATH
.
#!/usr/bin/env bash
bundle exec "$@"
followed up with (possibly with sudo
)
$ chmod +x /usr/local/bin/be
This is a bit of a naive example, and requires that both ruby
and the bundler
gem have been installed, but the thing to notice is "$@"
which let's you pass in a variable number of arguments into the script. Thus, you get the ergonomics of the alias, but with a command that is always available and not context dependent.
I find this approach particularly helpful when working with containers. Hope it helps.