I try to avoid installing specific versions of programming language interpreters and compilers on my operating system. Project A has other requirements as project B or C and the software stack on my operating system gets more and more confusing. Luckily is it possible to run the most command line based things in a Docker container. I will try to compile and run a small Java program in a Docker container.
Let us start with a small Hello World application in a HelloDocker.java
file.
public class HelloDocker {
public static void main(String[] args) {
System.out.println("Hello Docker");
}
}
We can compile this small application with the following command.
javac HelloDocker.java
And run it with the following command.
java HelloDocker
Hello Docker
We will use the openjdk image from OpenJDK. The command for compiling in the Docker container.
docker run --rm -v "$PWD":/usr/src/hello-docker -w /usr/src/hello-docker openjdk:8 javac HelloDocker.java
And the command for executing the application in the Docker container.
docker run --rm -v "$PWD":/usr/src/hello-docker -w /usr/src/hello-docker openjdk:8 java HelloDocker
Hello Docker