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

Jef*_*ide 25 sh ash docker alpine-linux

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

是否需要一些其他配置/etc/profile才能在登录时运行?我在使用~/.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
Run Code Online (Sandbox Code Playgroud)

虽然该/etc/profile.d/rubygems.sh文件存在,但是当我登录并且我的PATH环境变量没有被更新时它没有被运行.我使用了错误的docker run命令吗?还缺少什么?有没有人得到~/.profile/etc/profile.d/文件与Docker上的Alpine一起工作?谢谢!

小智 30

Alpine Linux中的默认shell是ash.

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

要强制Ash /etc/profile在调用它作为非登录shell时获取所需的任何其他脚本,您需要ENV在启动Ash之前设置一个调用的环境变量.

例如在你的Dockerfile中

FROM alpine:3.5

ENV ENV="/root/.ashrc"

RUN echo "echo 'Hello, world!'" > "$ENV"
Run Code Online (Sandbox Code Playgroud)

当你构建时,你得到:

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
Run Code Online (Sandbox Code Playgroud)

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

deployer@ubuntu-1604-amd64:~/blah$ docker run -ti test /bin/sh
Hello, world!
/ # exit
Run Code Online (Sandbox Code Playgroud)

注意,Ash shell没有作为登录shell运行.

所以要回答你的问题,请更换

ENV ENV="/root/.ashrc"
Run Code Online (Sandbox Code Playgroud)

有:

ENV ENV="/etc/profile"
Run Code Online (Sandbox Code Playgroud)

每次启动shell时,Alpine Linux的Ash shell都会自动获取/ etc/profile脚本.

问:/etc/profile通常只需要获取一次!所以,我建议您不要使用它,而是使用/root/.somercfile来代替.

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


Von*_*onC 21

您仍然可以在Dockerfile中尝试:

RUN echo '\
        . /etc/profile ; \
    ' >> /root/.profile
Run Code Online (Sandbox Code Playgroud)

(假设当前用户是root.如果不是,请/root用完整的主路径替换)

话虽这么说,那些/etc/profile.d/xx.sh应该运行.
codeclimate/docker-alpine-ruby作为一个例子:

COPY files /
Run Code Online (Sandbox Code Playgroud)

用'' files/etc包括files/etc/profile.d/rubygems.sh跑步就好了.


OP项目中 Dockerfile,有一个

COPY aliases.sh /etc/profile.d/
Run Code Online (Sandbox Code Playgroud)

但是默认shell 不是登录shell(sh -l),这意味着profile文件(或那些文件/etc/profile.d)不是来源的.

添加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
Run Code Online (Sandbox Code Playgroud)

  • `sh -l` 是缺失的部分!感谢您分享这一点! (2认同)

Cha*_*ell 5

如Jinesh之前所述,Alpine Linux中的默认外壳是ash

localhost:~$ echo $SHELL
/bin/ash
localhost:~$ 
Run Code Online (Sandbox Code Playgroud)

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

localhost:~$ cat .profile 
# ~/.profile

# Alias
if [ -f ~/.ash_aliases ]; then
    . ~/.ash_aliases
fi

localhost:~$ 
Run Code Online (Sandbox Code Playgroud)

.ash_aliases文件

localhost:~$ cat .ash_aliases
alias a=alias
alias c=clear
alias f=file
alias g=grep
alias l='ls -lh'
localhost:~$
Run Code Online (Sandbox Code Playgroud)

它有效:)


Ari*_*aco 5

我用这个:

docker exec -it my_container /bin/ash '-l'
Run Code Online (Sandbox Code Playgroud)

传递给 ash 的标志-l将使其表现为登录 shell,从而读取~/.profile