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?
问题:
回答1:
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\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
回答2:
To create an alias of an existing command, might also use ln -s
:
ln -s $(which <existing_command>) /usr/bin/<my_command>
回答3:
If you want to use aliases just in Dockerfile, but not inside container then the shortest way is ENV
declaration:
ENV update='apt-get update -qq'
ENV install='apt-get install -qq'
RUN $update && $install apt-utils \
curl \
gnupg \
python3.6
And for use in container the way like already described:
RUN printf '#!/bin/bash \n $(which apt-get) install -qq $@' > /usr/bin/install
RUN chmod +x /usr/bin/install
Most of the time I use aliases just on building stage and do not go inside containers, so first example is quicker, clearer and simpler for every day use.
回答4:
- edit this file ~/.bash_aliases
vi ~/.bash_aliases
- source this file ~/.bash_aliases
source ~/.bash_aliases
- done.
回答5:
You can use entrypoint, but it will not work for alias, in your Dockerfile:
ADD dev/entrypoint.sh /opt/entrypoint.sh
ENTRYPOINT ["/opt/entrypoint.sh"]
Your entrypoint.sh
#!/bin/bash
set -e
function dev_run()
{
}
export -f dev_run
exec "$@"
(Quick copy/paste, sorry)
回答6:
I just added this to my app.dockerfile
# setup aliases
ADD ./bashrc_alias.sh /usr/sbin/bashrc_alias.sh
ADD ./initbash_profile.sh /usr/sbin/initbash_profile
RUN chmod +x /usr/sbin/initbash_profile
RUN /bin/bash -C "/usr/sbin/initbash_profile"
and inside the initbash_profile.sh
which just appends my custom aliases and no need to source the .bashrc file.
# add the bash aliases
cat /usr/sbin/bashrc_alias.sh >> ~/.bashrc
worked a treat!
Another option is to just use the "docker exec -it command" from outside the container and just use your own .bashrc or the .bash_profile (what ever you prefer)
eg. docker exec -it docker_app_1 bash
回答7:
I think the easiest way would be to mount a file into your container containing your aliases, and then specify where bash should find it:
docker run \
-it \
--rm \
-v ~/.bash_aliases:/tmp/.bash_aliases \
[image] \
/bin/bash --init-file /tmp/.bash_aliases
Sample usage:
user@cobalt:~$ echo 'alias what="echo it works"' > my_aliases
user@cobalt:~$ docker run -it --rm -v ~/my_aliases:/tmp/my_aliases ubuntu:18.04 /bin/bash --init-file /tmp/my_aliases
root@565e4a1bdcc0:/# alias
alias what='echo it works'
root@565e4a1bdcc0:/# what
it works