如何让/ etc / profile在Alpine / Docker中自动运行

如何在/etc/profile交互式启动Alpine Docker容器时自动运行? 我已经添加了一些别名到一个aliases.sh文件,并将其放在/etc/profile.d ,但是当我使用docker run -it [my_container] sh启动容器时,我的别名不是活动的。 我必须手动input. /etc/profile . /etc/profile每次从命令行。

是否有一些其他configuration需要/etc/profile在login时运行? 我也有使用~/.profile文件的问题。 任何见解都被赞赏!

编辑:

基于VonC的回答,我拉着他的例子ruby容器。 这是我得到的:

 $ docker run --rm --name ruby -it codeclimate/alpine-ruby:b42 / # more /etc/profile.d/rubygems.sh export PATH=$PATH:/usr/lib/ruby/gems/2.0.0/bin / # env no_proxy=*.local, 169.254/16 HOSTNAME=6c7e93ebc5a1 SHLVL=1 HOME=/root TERM=xterm PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin PWD=/ / # exit 

尽pipe/etc/profile.d/rubygems.sh文件存在,但是当我login并且我的PATH环境variables没有被更新时,它不会被运行。 我使用错误的docker run命令? 还有其他的东西丢失吗 有没有人得到~/.profile/etc/profile.d/文件在Docker上使用Alpine? 谢谢!

你仍然可以在你的Dockerfile中尝试一下:

 RUN echo '\ . /etc/profile ; \ ' >> /root/.profile 

(假设当前用户是root用户,如果没有,用完整的homepathreplace/root

这就是说,那些/etc/profile.d/xx.sh应该运行。
codeclimate/docker-alpine-ruby为例:

 COPY files / 

用' files/etc '包含一个files/etc/profile.d/rubygems.sh运行得很好。


在OP项目 Dockerfile ,有一个

 COPY aliases.sh /etc/profile.d/ 

但是默认的shell 不是一个loginshell(sh -l),这意味着profile文件(或者/etc/profile.d profile文件) 不是来源的 。

添加sh -l将工作:

 docker@default:~$ docker run --rm --name ruby -it codeclimate/alpine-ruby:b42 sh -l 87a58e26b744:/# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/ruby/gems/2.0.0/bin 

Alpine Linux的默认shell是ash

如果它作为loginshell sh -l启动,Ash将只读取/etc/profile~/.profile文件。

要迫使Ash在调用时将/etc/profile或任何其他脚本作为非loginshell发送,需要在启动Ash之前设置一个名为ENV的环境variables。

例如在您的Dockerfile中

 FROM alpine:3.5 ENV ENV="/root/.ashrc" RUN echo "echo 'Hello, world!'" > "$ENV" 

当你build立你得到:

 deployer@ubuntu-1604-amd64:~/blah$ docker build --tag test . Sending build context to Docker daemon 2.048kB Step 1/3 : FROM alpine:3.5 3.5: Pulling from library/alpine 627beaf3eaaf: Pull complete Digest: sha256:58e1a1bb75db1b5a24a462dd5e2915277ea06438c3f105138f97eb53149673c4 Status: Downloaded newer image for alpine:3.5 ---> 4a415e366388 Step 2/3 : ENV ENV "/root/.ashrc" ---> Running in a9b6ff7303c2 ---> 8d4af0b7839d Removing intermediate container a9b6ff7303c2 Step 3/3 : RUN echo "echo 'Hello, world!'" > "$ENV" ---> Running in 57c2fd3353f3 ---> 2cee6e034546 Removing intermediate container 57c2fd3353f3 Successfully built 2cee6e034546 

最后,当你运行新生成的容器时,你会得到:

 deployer@ubuntu-1604-amd64:~/blah$ docker run -ti test /bin/sh Hello, world! / # exit 

请注意,Ash壳不会作为loginshell运行。

所以要回答你的查询,请replace

 ENV ENV="/root/.ashrc" 

有:

 ENV ENV="/etc/profile" 

和Alpine Linux的Ash shell将在每次启动shell时自动获取/ etc / profile脚本。

问题: / etc / profile通常只能被find一次! 所以,我build议你不要源代码,而是改用源代码/root/.somercfile。

来源: https : //stackoverflow.com/a/40538356

正如之前的Jinesh所提到的,Alpine Linux的默认shell是ash

 localhost:~$ echo $SHELL /bin/ash localhost:~$ 

因此,简单的解决scheme也是在.profile中添加别名。 在这种情况下,我把所有的别名放在〜/ .ash_aliases中

 localhost:~$ cat .profile # ~/.profile # Alias if [ -f ~/.ash_aliases ]; then . ~/.ash_aliases fi localhost:~$ 

.ash_aliases文件

 localhost:~$ cat .ash_aliases alias a=alias alias c=clear alias f=file alias g=grep alias l='ls -lh' localhost:~$ 

它的工作:)