简体   繁体   English

将docker run容器ID管道传输到docker exec

[英]Piping docker run container ID to docker exec

In my development, I find myself issuing a docker run command followed by a docker exec command on the resulting container ID quite frequently. 在我的开发中,我发现自己经常在生成的容器ID上发出docker run命令,然后发出docker exec命令。 It's a little annoying to have to copy/paste the container ID between commands, so I was trying to pipe the container ID into my docker exec command. 必须在命令之间复制/粘贴容器ID有点烦人,所以我试图将容器ID传递到我的docker exec命令中。

Here's my example command. 这是我的示例命令。

docker run -itd image | xargs -i docker exec -it {} bash

This starts the container, but then I get the following error. 这将启动容器,但是随后出现以下错误。

the input device is not a TTY

Does anyone have any idea how to get around this? 有谁知道如何解决这个问题?

Edit: I also forgot to mention I have an ENTRYPOINT defined and cannot override that. 编辑:我也忘了提我定义了一个ENTRYPOINT,并且不能覆盖它。

Do this instead: 改为这样做:

ID=$(docker run -itd image)  && docker exec -it $ID bash

Because xargs executes it arguments without allocating a new tty. 因为xargs在不分配新tty的情况下执行it参数。

If you just want to "bash"-into the container you do not have to pass the container-id around. 如果您只是想“扑食”到容器中,则不必传递容器ID。 You can simply run 您可以简单地运行

docker run -it --rm <image> /bin/bash

For example, if we take the ubuntu base image 例如,如果我们拍摄ubuntu基本图像

docker run -it --rm ubuntu /bin/bash
root@f80f83eec0d4:/#

from the documentation 文档中

-t : Allocate a pseudo-tty -t:分配一个伪tty
-i : Keep STDIN open even if not attached -i:即使未连接,也请保持STDIN处于打开状态
--rm : Automatically remove the container when it exits --rm:退出时自动删除容器

The command /bin/bash overwrites the default command that is specified with the CMD instruction in the Dockerfile. / bin / bash命令将覆盖 Dockerfile中使用CMD指令指定的默认命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM