首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Dockerfile中为docker容器设置Bash别名?

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

Stack Overflow用户
提问于 2016-04-04 01:09:22
回答 8查看 87.8K关注 0票数 97

我是Docker的新手。我发现我们可以使用Dockerfile中的ENV指令来设置环境变量。但是如何在Dockerfile中为长命令设置Bash别名呢?

EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2016-04-04 01:44:41

基本上像往常一样,将其添加到用户的.bashrc文件中:

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

像往常一样,这只适用于交互式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

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

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

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

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

Stack Overflow用户

发布于 2017-07-12 02:47:10

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

ln -s $(which <existing_command>) /usr/bin/<my_command>
票数 16
EN

Stack Overflow用户

发布于 2018-07-18 06:13:07

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

ENV update='apt-get update -qq'
ENV install='apt-get install -qq'

RUN $update && $install apt-utils \
    curl \
    gnupg \
    python3.6

对于容器中的使用,就像前面描述的那样:

 RUN printf '#!/bin/bash \n $(which apt-get) install -qq $@' > /usr/bin/install
 RUN chmod +x /usr/bin/install

大多数时候,我只是在构建阶段使用别名,而不是进入容器内部,因此第一个示例更快、更清晰、更简单,便于日常使用。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36388465

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档