43

如何/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

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

4

4 回答 4

60

Alpine Linux 中的默认 shell 是ash.

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

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

例如在你的 Dockerfile

FROM alpine:3.5

ENV ENV="/root/.ashrc"

RUN echo "echo 'Hello, world!'" > "$ENV"

当您构建时,您会得到:

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 shell 没有作为登录 shell 运行。

因此,要回答您的查询,请替换

ENV ENV="/root/.ashrc"

和:

ENV ENV="/etc/profile"

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

问题: /etc/profile 通常意味着只能获取一次!因此,我建议您不要使用它,而是使用 /root/.somercfile 代替。

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

于 2017-05-02T17:04:30.420 回答
37

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

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

(假设当前用户是root。如果不是,则替换/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不是登录 shell (sh -l),这意味着profile文件(或 中的文件/etc/profile.d不是sourced

添加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
于 2016-06-25T06:28:39.547 回答
7

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

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

因此,简单的解决方案也是在 .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:~$

它有效:)

于 2017-06-16T05:02:11.667 回答
1

我用这个:

docker exec -it my_container /bin/ash '-l'

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

于 2022-01-14T17:28:12.120 回答