如何在 Dockerfile 中为 docker 容器设置 bash 别名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36388465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:13:34  来源:igfitidea点击:

How to set bash aliases for docker containers in Dockerfile?

bashunixdockeraliasdockerfile

提问by np20

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?

我是码头工人的新手。我发现我们可以使用 Dockerfile 中的 ENV 指令设置环境变量。但是如何在 Dockerfile 中为长命令设置 bash 别名?

回答by Erik Dannenberg

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

基本上就像你往常一样,通过将它添加到用户的.bashrc

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

As usual this will only work for interactive shells:

像往常一样,这仅适用于交互式 shell:

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.:

对于非交互式 shell,您应该创建一个小脚本并将其放在您的路径中,即:

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 "$@":

如果您的别名使用参数(即hi Jim-> hello Jim),只需添加"$@"

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

回答by Laurent Magnin

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

要创建现有命令的别名,也可以使用ln -s

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

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

回答by Sonique

If you want to use aliases just in Dockerfile, but not inside container then the shortest way is ENVdeclaration:

如果您只想在 Dockerfile 中使用别名,而不是在容器内使用别名,那么最短的方法是ENV声明:

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.

大多数情况下,我只在构建阶段使用别名,不会进入容器,因此第一个示例在日常使用中更快、更清晰、更简单。

回答by mikoop

I just added this to my app.dockerfile

我刚刚将它添加到我的 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.shwhich just appends my custom aliases and no need to source the .bashrc file.

并在initbash_profile.sh其中仅附加我的自定义别名而无需提供 .bashrc 文件。

# 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)

另一种选择是仅使用容器外部的“docker exec -it 命令”,并使用您自己的 .bashrc 或 .bash_profile (您喜欢什么)

eg. docker exec -it docker_app_1 bash

例如。 docker exec -it docker_app_1 bash

回答by Gillespie

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:

我认为最简单的方法是将文件挂载到包含别名的容器中,然后指定 bash 应该在哪里找到它:

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

回答by Eddy Ferreira

  1. edit this file ~/.bash_aliases vi ~/.bash_aliases
  2. source this file ~/.bash_aliases source ~/.bash_aliases
  3. done.
  1. 编辑这个文件 ~/.bash_aliases vi ~/.bash_aliases
  2. 源这个文件 ~/.bash_aliases source ~/.bash_aliases
  3. 完毕。

回答by Thomas Decaux

You can use entrypoint, but it will not work for alias, in your Dockerfile:

您可以使用入口点,但它不适用于 Dockerfile 中的别名:

ADD dev/entrypoint.sh /opt/entrypoint.sh
ENTRYPOINT ["/opt/entrypoint.sh"]

Your entrypoint.sh

您的 entrypoint.sh

#!/bin/bash
set -e

function dev_run()
{

}

export -f dev_run

exec "$@"

(Quick copy/paste, sorry)

(快速复制/粘贴,抱歉)