How to set bash aliases for docker containers in D

2020-02-17 04:30发布

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?

7条回答
神经病院院长
2楼-- · 2020-02-17 05:08

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
查看更多
Bombasti
3楼-- · 2020-02-17 05:09
  1. edit this file ~/.bash_aliases vi ~/.bash_aliases
  2. source this file ~/.bash_aliases source ~/.bash_aliases
  3. done.
查看更多
来,给爷笑一个
4楼-- · 2020-02-17 05:11

To create an alias of an existing command, might also use ln -s:

ln -s $(which <existing_command>) /usr/bin/<my_command>

查看更多
女痞
5楼-- · 2020-02-17 05:11

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.

查看更多
▲ chillily
6楼-- · 2020-02-17 05:20

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楼-- · 2020-02-17 05:20

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
查看更多
登录 后发表回答