Answer a question

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

Answers

Basically like you always do, by adding it to the user's .bashrc file:

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
hello

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

RUN echo -e '#!/bin/bash\necho hello' > /usr/bin/hi && \
    chmod +x /usr/bin/hi

If your alias uses parameters (ie. hi Jim -> hello Jim), just add "$@":

RUN echo -e '#!/bin/bash\necho hello "$@"' > /usr/bin/hi && \
    chmod +x /usr/bin/hi
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐