可以在Docker容器中安装ASDF,但可以通过Dockerfile在构建时间安装
我有一个Dockerfile,我正在尝试安装和使用ASDF来管理Python软件包版本。我的Dockerfile的片段出现在下面。
SHELL ["/bin/bash", "-c"]
RUN git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.10.0
RUN chmod +x ~/.asdf/asdf.sh ~/.asdf/completions/asdf.bash
RUN echo ". $HOME/.asdf/asdf.sh" >> ~/.bashrc
RUN echo ". $HOME/.asdf/completions/asdf.bash" >> ~/.bashrc
ENV PATH="$HOME/.asdf/bin:$HOME/.asdf/shims:$PATH"
ENV PATH="$HOME/.asdf:$PATH"
RUN echo -e '\nsource $HOME/.asdf/asdf.sh' >> ~/.bashrc
RUN source ~/.bashrc
RUN bash -c 'echo -e which asdf'
RUN asdf plugin-add python
最后一行是有问题的线。当我尝试构建此Docker映像时,我会得到以下内容。
=> ERROR [17/19] RUN asdf plugin-add python 0.3s
------
> [17/19] RUN asdf plugin-add python:
#21 0.292 /bin/bash: asdf: command not found
------
executor failed running [/bin/bash -c asdf plugin-add python]: exit code: 127
但是,如果删除该行,我可以运行一个容器,然后立即成功运行ASDF。
docker run -it <image ID>
root:# asdf plugin-add python
initializing plugin repository...Cloning into '/root/.asdf/repository'...
<etc>
当我尝试通过Dockerfile运行它时,为什么这不起作用?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我发现
source〜/.bashrc;
并不总是在docker容器中执行此操作(有时不在实时操作系统中),也不是<代码>/bin/bash -c'source〜/.bashrc'; 。我发现,在构建时间安装
ASDF
的诀窍是,我发现要远至重新启动bash (即exec exec bash
)。这可能与我们实际上没有修改path =
以及随后source&lt; file&gt;
'之类的事实有关,而是使用POSIX兼容的源指令以文件名(减去其扩展名)来吸用可执行脚本。。 $ home/.asdf/asdf.sh
表示:
source/home/home/.asdf/asdf.sh
,它似乎给出了:
别名asdf =/home/home/home/home/home/用户/.asdf/asdf.sh
简单地采购文件无法正常工作,因为安装说明(ASDF作者提供)基本上提供了一个源指令,以放置
〜/.bashrc
,我相信,与我们使用的外壳相比,上下文成为了一个。要解决此问题,我们必须重新启动bash-别无其他方式。
我们还会遇到很多怪癖,并且在尝试将用户软件包(实际上只是语义)配置为root时必须规定的问题,因此,为了避免,最好建立一个非根源用户。
这是一个工作示例,可以进一步安装Ruby和nodejs:
I found that
source ~/.bashrc;
just doesn't always do it in a docker container (it doesn't in a live OS at times), nor does/bin/bash -c 'source ~/.bashrc';
.The trick to installing
asdf
in a Docker container at build time, I found, was to go as far as to restart bash (i.e.exec bash
). Possibly it has to do with the fact that we're not actually modifying something likePATH=
and subsequentlysource <file>
'ing it, but using posix compliant source directives to alias an executable script by the filename (minus its extension).. $HOME/.asdf/asdf.sh
Means:
source /home/user/.asdf/asdf.sh
Which seemingly gives the effect of:
alias asdf=/home/user/.asdf/asdf.sh
Simply sourcing the file won't work because the install instructions (provided by asdf authors) provide essentially a source directive to place inside of
~/.bashrc
, and I believe the context then becomes one other than the one the shell we're using is subsequently under.To fix this, we have to restart bash - there's no other way.
We'll also run into a lot of quirks and issues we have to circumvent when trying to configure user packages (just semantics, really) as root, so to avoid that its best to establish a non-root user to work with.
Here's a working example that goes further to install Ruby and NodeJS:
exec bash
此答案中的建议对我不起作用,但是我确实发现您可以在dockerfile中指定外壳,并且每个运行命令将坚持使用bash更改。前任。shell [“/bin/bash”,“ - lc”]
感谢此Github用户和要点向我展示此信息,这也是一个广泛的ASDF Dockerfile。
The
exec bash
suggestion in this answer wasn't working for me, but I did find you can specify the shell in your dockerfile and bash changes will be persisted with each RUN command. Ex.SHELL ["/bin/bash", "-lc"]
Thanks to this github user and gist for showing me this, also it is a pretty extensive asdf Dockerfile. https://gist.github.com/BrutalSimplicity/882af1d343b7530fc7e005284523d38d
这个...
绝对什么都不做。每个
run
命令在新的shell中执行,该命令在命令完成时退出。采购bash脚本,设置变量和其他修改当前环境的事物不会持续到后续运行
命令。您可以修改
dockerfile
以在运行
命令中运行一系列命令,例如:...它至少会成功地源自
.bashrc 文件,大概使
asdf
工具可用。This...
Does absolutely nothing. Each
RUN
command executes in a new shell, which exits when the command completes. Sourcing bash scripts, setting variables, and other things that modify the current environment will not persist to subsequentRUN
commands.You could modify your
Dockerfile
to run a sequence of commands in aRUN
command, like this:...which would at least successfullly source the
.bashrc
file and presumably make theasdf
tool available.我发现,克隆
asdf
要安装它后,获得asdf
命令的最简单方法是通过设置path
来包括:完整示例:
我还建议您查看更强大的配置。您可以将其用作基本图像,也可以将其命令阅读以获得灵感。
I have found that after cloning
asdf
to install it, the easiest way to get theasdf
command to run is by settingPATH
to include it:Full example:
I also recommend looking at this Dockerfile which has a more robust configuration. You can either use that as a base image, or just read its commands for inspiration.
如前所述,运行
无济于事 - 该文件是针对其他外壳实例的。将Shell设置为使用
- 登录
选项bash bash bash命令后,每个实例都可以读取配置文件。因此,您的
dockerfile
变为:您也不需要其他几行,在调试
dockerfile
;-)更新时很可能会添加它们:
显然您的配置链文件需要源
.bashrc
。 事情类似于
.bash_profile
应该做的 。或者使用.bash_profile
而不是.bashrc
也应该这样做。bash
InvocationAs mentioned already, running
doesn't help – the file is sourced for a different shell instance. Setting shell to bash with
--login
option makes every instance aftercommand to read configuration files though. Therefore your
Dockerfile
becomes:As you also don't need those few other lines, which you most probably added while debugging your
Dockerfile
;-)Update:
obviously your chain of config files needs to source
.bashrc
. Something likein
.bash_profile
should do. Alternatively using.bash_profile
instead of.bashrc
should also do. More information inbash
man page - sectionINVOCATION