Docker运行bash --init-file [英] Docker run bash --init-file
本文介绍了Docker运行bash --init-file的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个别名来帮助调试Docker容器。
I'm trying to create an alias to help debug my docker containers.
我发现bash 接受-init-file
选项,该选项应允许我们在传递到交互模式之前运行一些命令。
I discovered bash accepts a --init-file
option which ought to let us run some commands before passing over to interactive mode.
所以我想我可以做
docker-bash() {
docker run --rm -it "$1" bash --init-file <(echo "ls; pwd")
}
但是这些命令似乎没有在运行:
But those commands don't appear to be running:
% docker-bash c7460dfcab50
root@9c6f64a9db8c:/#
这是逃避问题还是..怎么回事
Is it an escaping issue or.. what's going on?
bash --init-file <(echo "ls; pwd")
在我主机上的终端中,一个单独的终端可以正常工作(运行命令启动一个新的bash实例)。 b
Alone in a terminal on my host machine works as expected (runs the command starts a new bash instance).
推荐答案
要点:
-
<(...)
我bash扩展流程替代。 - 从上面的手册中:
在支持命名管道(FIFO)或/ dev / fd命名打开文件的方法的系统上支持进程替换。
- 过程替换的工作方式如下:
- bash在
/ tmp中创建了一个fifo
或在/ dev / fd
中创建一个新的文件描述符。 - 文件名,即
/tmp/.something
或/ dev / fd /
代替< (...)
在执行命令时。 - 因此,例如
echo<(echo 1)
输出/ dev / fd / 63
。
- The
<(...)
is a bash extension process subtitution. - From the manual above:
Process substitution is supported on systems that support named pipes (FIFOs) or the /dev/fd method of naming open files.
. - The process substitution works like this:
- bash creates a fifo in
/tmp
or creates a new file descriptor in/dev/fd
. - The filename, either the
/tmp/.something
or/dev/fd/<number>
is substituted for<(...)
when command is executed. - So for example
echo <(echo 1)
outputs/dev/fd/63
.
- 泊坞窗内部的进程不会从主机进程继承文件描述符:
- 因此
/ dev / fd / *
文件不会被继承。
- Processes inside docker do not inherit file descriptors from the host process:
- So
/dev/fd/*
files are not inherited.
- 因此进程无法从主机访问
/ tmp / *
文件。
- So processes can't access
/tmp/*
files from the host.
一个简单的解决方法是:
An easy workaround would be to just:
docker run -ti --rm alpine sh -c 'ls; pwd; exec sh'
或使用临时文件:
echo "ls; pwd" > /tmp/tempfile docker run -v /tmp/tempfile:/tmp/tempfile bash bash --init-file /tmp/tempfile
这篇关于Docker运行bash --init-file的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- So
- 因此
- bash creates a fifo in
- bash在
查看全文