如何在Dockerfile中为Docker容器设置bash别名? [英] How to set bash aliases for docker containers in Dockerfile?

查看:520
本文介绍了如何在Dockerfile中为Docker容器设置bash别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

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

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

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

通常,这仅适用于交互式shell:

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

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

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

这篇关于如何在Dockerfile中为Docker容器设置bash别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆