gpt4 book ai didi

docker-java equivalent for 'docker exec {container name} touch test.txt'(Docker-Java对‘docker exec{tainer name}ouch test.txt’的等价物)

转载 作者:bug小助手 更新时间:2023-10-24 22:03:39 30 4
gpt4 key购买 nike



I am trying to run an exec command on an already running docker container on a remote host using java.

我正在尝试使用Java在远程主机上已经运行的停靠容器上运行一个exec命令。


I am successfully able to run the exec command from the CLI of the host machine using

我可以使用以下命令成功地从主机的CLI运行exec命令


docker exec {container-id} touch asd

and can run the touch command successfully.

并且可以成功运行触摸命令。


However, when I try to run the same using docker-java, the program doesn't throw any exceptions, but doesn't run the touch command.

然而,当我尝试使用docker-Java运行相同的命令时,程序不会抛出任何异常,但也不会运行Touch命令。


            var exec = dockerClient.execCreateCmd(containerId)
.withCmd("touch asd")
.exec();

var respo = dockerClient
.execStartCmd(exec.getId())
.exec(new ResultCallback.Adapter<>())

更多回答

You want withCmd("touch", "asd") -- when you run docker exec ... touch asd, the shell splits touch and asd into two separate arguments; when you're writing Java code you need to do that yourself.

您希望使用Cmd(“Touch”,“asd”)--当您运行docker exec...Touch ASD,外壳将Touch和ASD拆分成两个独立的参数;当您编写Java代码时,您需要自己做这件事。

Hmm interesting, looks like it works. Do you know if escaping the space will work? withCmd("touch\\ asd")

嗯,有趣,看起来它起作用了。你知道逃离这个空间会不会奏效?With Cmd(“触摸\\asd”)

No, it certainly won't. Escaping is shell syntax, but there is no shell here to honor that syntax. (And even if there were a shell: in this context, backslashes hold things together, which is the opposite of what you want: you want them split apart)

不,它肯定不会。转义是外壳语法,但这里没有外壳来遵守这种语法。(即使有壳:在这种情况下,反斜杠将事物结合在一起,这与你想要的相反:你想要它们分开)

优秀答案推荐

When you run docker exec ... touch asd, your shell splits touch and asd into two separate arguments.

当你运行Docker Exec时...触摸ASD,您的外壳将Touch和ASD分成两个独立的参数。


Using the Java library, there is no shell, so the arguments never get split (causing the system to try to run a program named like "/usr/bin/touch asd" with the space as part of its name in the container). It's on you to do that splitting yourself:

使用Java库时,没有外壳,因此参数永远不会拆分(导致系统尝试运行一个名为“/usr/bin/ouch asd”的程序,并在容器中使用空格作为其名称的一部分)。把你自己分开是你的责任:


var exec = dockerClient.execCreateCmd(containerId)
.withCmd("touch", "asd")
.exec()

...or to explicitly cause a shell to be run inside the container to do it for you:

...或显式地使外壳程序在容器内运行,以便为您执行此操作:


var exec = dockerClient.execCreateCmd(containerId)
.withCmd("sh", "-c", "touch asd")
.exec()

更多回答

Thanks for this, it works as expected with other commands as well. If I may ask one more favor, could you please redirect me to some resources where I could learn about reading output from exec commands in my java program. The above program runs successfully only after I add a Thread.sleep(2000) after it. I would like to avoid it.

多亏了这一点,它与其他命令的工作也与预期一样。如果我可以再请求一个帮助,您可以将我重定向到一些资源,在那里我可以了解如何在我的Java程序中读取exec命令的输出。上面的程序只有在我添加了一个Thread.sleep(2000)之后才能成功运行。我想要避免这种情况。

Sorry -- my recent expertise is on the shell side, not the Java side. You'll probably want to ask a separate question for that one. (As someone not particularly familiar with the API, it sounds like you're doing a non-blocking read but need a blocking one, if that helps).

对不起--我最近的专业知识是在外壳方面,而不是在Java方面。对于这个问题,您可能想单独问一个问题。(作为对API不是特别熟悉的人,听起来您正在进行非阻塞读取,但需要阻塞读取,如果这有帮助的话)。

No worries. Much much appreciated

别担心。非常感谢

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com