Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to run docker commands through java 19

user-o2pr1Apr 11 2023

Hello!

I am trying to create a custom docker container using java. I am using linux 22.04 and Java 19. I have been trying to do it in multiple ways, the first ways:

Runtime.getRuntime().exec(new String[]{"cd", "/home/alex/IdeaProjects/test/src/Docker", "&& docker", "build", " -f", "Dockerfile.txt", "-t", "java-app1", "."});

The second ways:

ProcessBuilder processBuilder = new ProcessBuilder(new String[]{"cd", "/home/alex/IdeaProjects/test/src/Docker", "&& docker", "build", " -f", "Dockerfile.txt", "-t", "java-app1", "."});
processBuilder.inheritIO();
processBuilder.directory(new File("bin"));
processBuilder.start();

I have been running linux commands using java, as in this example:

try {
Process p = Runtime.getRuntime().exec(new String[]{"bash", "-c", "cd " + folder + "&& find . -type f | grep :*.txt "});
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
return stdInput.lines().count();
} catch (IOException e) {
throw new RuntimeException(e);
}

Everything is working for the example.

Do you know a way that allows me to execute docker commands using java?

Comments
Post Details
Added on Apr 11 2023
1 comment
947 views