docker run -i -t image /bin/bash - 首先是源文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23031753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:11:10  来源:igfitidea点击:

docker run -i -t image /bin/bash - source files first

bashdocker

提问by mattes

This works:

这有效:

# 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:/# 

Question: How could I sourcea file into the shell? (this does not work)

问题:我怎样才能把source一个文件放入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:/# 

回答by NHK

In my case, I use RUN sourcecommand (which will run using /bin/bash) in a Dockerfile to install nvm for node.js

就我而言,我RUN source在 Dockerfile 中使用命令(将使用 /bin/bash 运行)为 node.js 安装 nvm

Here is an example.

这是一个例子。

FROM ubuntu:14.04

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

回答by Daniel Mahu

I wanted something similar, and expanding a bit on your idea, came up with the following:

我想要一些类似的东西,并扩展了你的想法,想出了以下内容:

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/1001will use that file descriptor's contents instead of .bashrc
  • 1002<&0saves stdin
  • <<<$(echo PS1=it_worked: )puts PS1=it_worked:on stdin
  • 1001<&0moves this stdin to fd 1001, which we use as rcfile
  • 0<&1002restores the stdin that we saved initially
  • --rcfile /dev/fd/1001将使用该文件描述符的内容而不是 .bashrc
  • 1002<&0保存标准输入
  • <<<$(echo PS1=it_worked: )穿上PS1=it_worked:标准输入
  • 1001<&0将此标准输入移动到 fd 1001,我们将其用作 rcfile
  • 0<&1002恢复我们最初保存的标准输入

回答by Erik Dannenberg

You can use .bashrcin interactive containers:

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

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

Then just run as usual with docker run -it --rm some/image bash.

然后像往常一样运行docker run -it --rm some/image bash

Note that this will onlywork with interactive containers.

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

回答by Michael Sauter

I don't think you can do this, at least not right now. What you could do is modify your image, and add the file you want to source, like so:

我认为你不能这样做,至少现在不能。您可以做的是修改您的图像,并添加您想要获取的文件,如下所示:

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