I am trying to run an exec command on an already running docker container on a remote host using java.
I am successfully able to run the exec command from the CLI of the host machine using
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.
var exec = dockerClient.execCreateCmd(containerId)
.withCmd("touch asd")
.exec();
var respo = dockerClient
.execStartCmd(exec.getId())
.exec(new ResultCallback.Adapter<>())
When you run docker exec ... touch asd
, your shell splits touch
and asd
into two separate arguments.
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:
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()