Docker exec doesn’t load aliases

aliasbashcontainerdocker

I have a bash alias loaded into a Docker container, in /etc/bash.bashrc.
It functions as a shortcut to a commandline PHP script.
This is convenient, since anyone can use that alias directly after one logs into the container, with:

$ docker exec -it my-container bash

However, I would also like to be able to use this alias in one-off commands without logging in, like:

$ docker exec -it my-container my-alias

I have tried different variations, such as defining the alias in other places than /etc/bash.bashrc, but I keep running into this error:

rpc error: code = 2 desc = oci runtime error: exec failed: exec: "my-alias": executable file not found in $PATH

Other suggestions I find on the web did not do the trick, so far. Anyone?

Best Answer

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!
Related Question