我正在Jenkinsfile中运行以下命令。然而,我收到错误“输入设备不是TTY”。

docker run -v $PWD:/foobar -it cloudfoundry/cflinuxfs2 /foobar/script.sh

有没有一种方法可以在不进行交互模式的情况下从Jenkinsfile运行脚本?

我基本上有一个名为script.sh的文件,我想在Docker容器中运行。


当前回答

只要不指定要装载的卷(例如.:/mountpoint或${pwd}:/mountpoint

我找到的最佳解决方法是在Visual Code Studio中使用git bash插件,并使用终端启动和停止容器或docker compose。

其他回答

使用“gitbash”时,

1) 我执行以下命令:

docker exec -it 726fe4999627 /bin/bash

我有错误:

the input device is not a TTY.  If you are using mintty, try prefixing the command with 'winpty'

2) 然后,我执行命令:

winpty docker exec -it 726fe4999627 /bin/bash

我还有一个错误:

OCI runtime exec failed: exec failed: container_linux.go:344: starting container process caused "exec: \"D:/Git/usr/bin/
bash.exe\": stat D:/Git/usr/bin/bash.exe: no such file or directory": unknown

3) 第三,我执行:

winpty docker exec -it 726fe4999627 bash

它奏效了。

当我使用“powershell”时,一切都很好。

如果您在windows上使用gitbash,只需将

冬季的

在“码头线”之前:

winpty docker exec -it some_container bash

我知道这并不是直接回答眼前的问题,而是针对使用WSL运行Docker for windows和cmder或conemu的人。

诀窍不在于使用安装在windows上的Docker,而在于安装ubuntu/linux Docker。值得指出的是,您不能在WSL中运行Docker本身,但可以从linux Docker客户端连接到Docker for windows。

在Linux上安装Docker

sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-get install docker-ce

连接到端口2375上的Docker for windows,需要从Docker for windows中的设置启用该端口。

docker-H localhost:2375运行-it-v/mnt/c/code:/var/app-w“/var/app”centos:7

或者设置docker_host变量,这样可以省略-H开关

导出DOCKER_HOST=tcp://localhost:2375

现在您应该能够与tty终端会话交互连接。

如果使用windows,请尝试使用cmd,对我来说它是有效的。检查docker是否已启动。

为了让docker分配TTY(-t选项),当docker运行被调用时,您已经需要在TTY中。Jenkins不在TTY中执行其任务。

话虽如此,您在Jenkins中运行的脚本可能也希望在本地运行。在这种情况下,分配TTY非常方便,这样在本地运行时就可以发送类似ctrl+c的信号。

要解决此问题,请使脚本可以选择使用-t选项,如下所示:

test -t 1 && USE_TTY="-t" 
docker run ${USE_TTY} ...