np20 np20 - 4 years ago 335
Bash Question

How to set bash aliases for docker containers in Dockerfile?

I am new to docker. I found that we can set environment variables using ENV instruction in the Dockerfile. But how does one set bash aliases for long commands in Dockerfile?

Answer Source

Basically like you always do, by adding it to the users .bashrc:

FROM foo
RUN echo 'alias hi="echo hello"' >> ~/.bashrc

As usual this will only work for interactive shells:

docker build -t test .
docker run -it --rm --entrypoint /bin/bash test hi
/bin/bash: hi: No such file or directory
docker run -it --rm test bash
$ hi

For non-interactive shells you should create a small script and put it in your path, i.e.:

echo '#!/bin/bash\necho hello' > /usr/bin/hi
chmod +x /usr/bin/hi
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download