Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
675 views
in Technique[技术] by (71.8m points)

unix - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Basically like you always do, by adding it to the user's .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
hello

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

RUN echo -e '#!/bin/bash
echo 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
echo hello "$@"' > /usr/bin/hi && 
    chmod +x /usr/bin/hi

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...