docker运行-i -t image / bin / bash – 首先创build源文件

这工作:

# echo 1 and exit: $ docker run -i -t image /bin/bash -c "echo 1" 1 # exit # echo 1 and return shell in docker container: $ docker run -i -t image /bin/bash -c "echo 1; /bin/bash" 1 root@4c064f2554de:/# 

问题 :如何将文件input到shell中? (这不起作用)

 $ docker run -i -t image /bin/bash -c "source <(curl -Ls git.io/apeepg) && /bin/bash" # content from http://git.io/apeepg is sourced and shell is returned root@4c064f2554de:/# 

在我的情况下,我在Dockerfile中使用RUN source命令(将使用/ bin / bash运行)来为node.js安装nvm

这是一个例子。

 FROM ubuntu:14.04 RUN rm /bin/sh && ln -s /bin/bash /bin/sh ... ... RUN source ~/.nvm/nvm.sh && nvm install 0.11.14 

我想要类似的东西,并扩大一点你的想法,提出以下几点:

 docker run -ti --rm ubuntu \ bash -c 'exec /bin/bash --rcfile /dev/fd/1001 \ 1002<&0 \ <<<$(echo PS1=it_worked: ) \ 1001<&0 \ 0<&1002' 
  • --rcfile /dev/fd/1001将使用该文件描述符的内容而不是.bashrc
  • 1002<&0保存stdin
  • <<<$(echo PS1=it_worked: )PS1=it_worked:上的PS1=it_worked:
  • 1001<&0将这个stdin移动到fd 1001,我们用它作为rcfile
  • 0<&1002恢复最初保存的stdin

您可以在交互式容器中使用.bashrc

 RUN curl -O git.io/apeepg.sh && \ echo 'source apeepg.sh' >> ~/.bashrc 

然后像平常一样docker run -it --rm some/image bash

请注意, 这只适用于交互容器。

我不认为你能做到这一点,至less现在不行。 你可以做的是修改你的图像,并添加你想要的源文件,如下所示:

 FROM image ADD my-file /my-file RUN ["source", "/my-file", "&&", "/bin/bash"]