在构build期间将别名添加到Docker

因为我正在编译一个容器的编译阶段的程序,所以我在包含.bashrc中的容器的编译过程中join了别名:

RUN cat /path/to/aliases.sh >> ~/.bashrc 

当我启动容器时,所有别名都可用。 这已经很好了,但不是我想要的行为。

我已经谷歌周围发现,.bashrc文件只加载时使用交互式的壳,而不是在容器的生成阶段的情况下。

我试图强制加载我的别名使用:

 RUN shopt -s expand_aliases 

要么

 RUN shopt -s expand_aliases && alias 

要么

 RUN /bin/bash -c "both commands listed above..." 

令人惊讶的是没有达到预期的结果。 [/反讽]

现在我的问题:我怎样才能为容器的构build阶段设置别名?

问候

当docker执行每个RUN ,它会调用SHELL传递行的其余部分作为参数。 默认的shell是/bin/sh 。 logging在这里

这里的问题是你需要每一个层的执行,来设置别名,因为每个RUN启动一个新的shell。 我没有find一种非交互式的方式让bash每次读取.bashrc文件。

所以, 只是为了好玩,我做了这个,它的工作:

aliasshell.sh

 #!/bin/bash my_ls(){ ls $@ } $@ 

Dockerfile

 FROM ubuntu COPY aliasshell.sh /bin/aliasshell.sh SHELL ["/bin/aliasshell.sh"] RUN ls -l /etc/issue RUN my_ls -l /etc/issue 

输出

 docker build . Sending build context to Docker daemon 4.096 kB Step 1/5 : FROM ubuntu ---> f7b3f317ec73 Step 2/5 : COPY aliasshell.sh /bin/aliasshell.sh ---> Using cache ---> ccdfc54dd0ce Step 3/5 : SHELL /bin/aliasshell.sh ---> Using cache ---> bb17a8bf1c3c Step 4/5 : RUN ls -l /etc/issue ---> Running in 15ae8f0bb93b -rw-r--r-- 1 root root 26 Feb 7 23:55 /etc/issue ---> 0337da801651 Removing intermediate container 15ae8f0bb93b Step 5/5 : RUN my_ls -l /etc/issue <------- ---> Running in 5f58e0aa4e95 -rw-r--r-- 1 root root 26 Feb 7 23:55 /etc/issue ---> b5060d9c5e48 Removing intermediate container 5f58e0aa4e95 Successfully built b5060d9c5e48