如何让/etc/profile
在交互启动高山码头容器时自动运行?我已经向一个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/
文件在码头上的阿尔卑斯工作?谢谢!
发布于 2016-06-25 14:28:40
你仍然可以在你的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 project Dockerfile
中,有一个
COPY aliases.sh /etc/profile.d/
但是默认shell不是登录shell (sh -l),这意味着profile
files (or those in /etc/profile.d
) are 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
发布于 2017-05-03 01:04:30
Alpine中的默认外壳是ash
。
如果作为登录外壳sh -l
启动,则Ash将仅读取/etc/profile
和~/.profile
文件。
要强制Ash将/etc/profile
或任何其他脚本作为非登录shell调用,您需要在启动Ash之前设置一个名为ENV
的环境变量。
例如,在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"
在每次启动/etc/profile脚本时,高山Linux的Ash shell都会自动生成/etc/profile脚本。
Gotcha: /etc/profile通常只有一次来源!因此,我建议您不要获取它,而是获取一个/root/.somercfile。
发布于 2017-06-16 13:02:12
正如Jinesh之前所提到的,高山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:~$
它是有效的:)
https://stackoverflow.com/questions/38024160
复制相似问题