如何在 Dockerfile 中为 docker 容器设置 Bash 别名? [英] How can I set Bash aliases for docker containers in Dockerfile?

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

问题描述

I am new to Docker. I found that we can set environment variables using the ENV instruction in the Dockerfile. But how does one set Bash aliases for long commands in Dockerfile?

解决方案

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

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
echo 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
echo hello "$@"' > /usr/bin/hi && 
    chmod +x /usr/bin/hi

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

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