Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Docker 如何在shell脚本中将命令的输出传递给另一个命令_Docker_Shell_Ssh_Sh - Fatal编程技术网

Docker 如何在shell脚本中将命令的输出传递给另一个命令

Docker 如何在shell脚本中将命令的输出传递给另一个命令,docker,shell,ssh,sh,Docker,Shell,Ssh,Sh,我正试图通过ssh连接到服务器,并连接到docker容器中以运行该服务。但是,我无法将containerId存储到变量中,以便将其传递到容器中 #!/bin/bash ssh test_server << EOF ls sudo docker ps | grep 'tests_service_image' | colrm 13 # This command works containerId=$(sudo docker ps | grep '

我正试图通过ssh连接到服务器,并连接到docker容器中以运行该服务。但是,我无法将containerId存储到变量中,以便将其传递到容器中

#!/bin/bash

ssh test_server << EOF
 ls 
 sudo docker ps | grep 'tests_service_image' | colrm 13                 # This command works
 containerId=$(sudo docker ps | grep 'tests_service_image' | colrm 13)  # This doesn't
 sudo docker exec -i "$containerId" bash                           # Throws error since containerId is empty
 ./run.sh 
EOF

exit
#/bin/bash
ssh测试服务器编辑:
直接将其传递给docker exec命令,如下所示

sudo docker exec-i$(sudo docker ps | grep | tests | u service | u image | colrm 13)bash

原始答案

这是在假定脚本执行是在sshing进入服务器后完成的情况下编写的。但根据具体查询修改了上述答案


容器ID存储在变量
containerId
中,您会收到错误
错误:没有这样的容器:
,因为您正在将另一个变量
$container
而不是
$containerId
传递给docker exec命令。

问题是您正在自己进行变量/函数扩展。您需要避开这些,以便这些扩展在服务器端发生

#/垃圾箱/垃圾箱

ssh测试服务器这是一个输入错误,更正了。仍然不起作用。你能分享你得到的错误吗?由于containerId在传递到sudo docker exec命令时是空的,所以找不到该错误。不要将其存储在单独的变量中,直接传递给docker exec命令,如so
sudo docker exec-i$(sudo docker ps | grep“tests_service_image”| colrm 13)bash
echo$containerid以确保变量中有实际id。