0

My working environment is macOS 12.
UPDATE: Ubuntu 20.04 has the same issue.

I'm trying to create a Docker container and mount a volume to it.

Here's what I tried:

public class Test {

    public static void main(String[] args) throws IOException, InterruptedException {
        var p = Runtime.getRuntime().exec("docker run -d -it -v \"/Users/myname/Documents/MC_Servers/BungeeCord/plugins/PlayerServers/servers/OpenSourcee:/data\" -p 32348:32348 -e TYPE=PAPER -e ONLINE_MODE=false -e SERVER_PORT=32348 -e EULA=TRUE -e VERSION=1.8.8 --name OpenSourcee itzg/minecraft-server", null, null);

        // Read the output from the command
        var reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }

        System.out.println(p.waitFor());
    }
}

The output seems to be empty, and the exit value is 125

Interestingly enough, if I do the same, but without

-v \"/Users/myname/Documents/MC_Servers/BungeeCord/plugins/PlayerServers/servers/OpenSourcee:/data\"

it works without any issues. As an example, the following works:

public class Test {

    public static void main(String[] args) throws IOException, InterruptedException {
        var p = Runtime.getRuntime().exec("docker run -d -it -p 32348:32348 -e TYPE=PAPER -e ONLINE_MODE=false -e SERVER_PORT=32348 -e EULA=TRUE -e VERSION=1.8.8 --name OpenSourcee itzg/minecraft-server", null, null);

        // Read the output from the command
        var reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }

        System.out.println(p.waitFor());
    }
}

I did also try to manually execute the command from the first block and it works without any issues, however, it does not work from Java. I'm guessing it may be some kind of permission issue or something...?

OpenSource
  • 13
  • 4
  • I'd suggest using a [Docker SDK](https://docs.docker.com/engine/api/sdk/) over trying to run the `docker` CLI as a subprocess. Is there anything interesting on the subprocess's stderr? Can you use `Runtime.exec(String[])` to remove any ambiguity around quoting and shell parsing (and potential shell-injection attacks)? – David Maze Jun 14 '22 at 00:50
  • Hi and thank you very much for your suggestion. It seems like the issue was that I had \" in the command. For whatever reason, when running the command from Java it is possible to run it without quotes, but when running in terminal, the directory path must be inside quotes. I was easily able to find the issue using the stderr. Forgot that it exists . And yes, don't worry, this was just a sample code for testing purposes. – OpenSource Jun 14 '22 at 00:57

1 Answers1

0

The issue seems to be the fact I used ", but it was not required, and furthermore, it caused issues. While with docker command in terminal quotes seem to be required, in Java, it seems to be the opposite.

For anyone with a similar problem, I'd suggest also checking out p.getErrorStream() and proceed debugging errors from there.

OpenSource
  • 13
  • 4