小编典典

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

docker

/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一起使用?谢谢!


阅读 822

收藏
2020-06-17

共1个答案

小编典典

您仍然可以在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/

但是默认外壳 不是 登录外壳(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
2020-06-17