Detailed explanation of the method of building a simple java development and compilation environment by using Docker

  • 2021-09-11 21:49:59
  • OfStack

At present, there are many versions of Java language. Apart from the commonly used Java 8, there are 1 legacy project that may use Java 7, and 1 relatively new project that may use Java 10 or more. If you want to switch your own local Java development environment, it will take 1 time to toss, and you will have to toss once every time you switch between different versions in the future.

The appearance of Docker makes it easy for us to maintain different versions of the development and compilation environment. If you don't know what Docker is, you can see the introduction of Docker. We can use two ways to build the development environment of java, one is compiled and run in the container, and the other is compiled and run outside the container. Let's take a look at how to operate them respectively.

Preparation: First, make sure you have installed Docker. If it is convenient, you can put it in advance openjdk:8 The mirror image is pulled down, which can save everyone's time. This paper has been verified in the environment of macOs 10.15. 7 and Docker 19.03. 8.

Compile and run in container

This paper takes a simple Helloworld program as an example, and the file directory structure and code of the project are as follows.


$ ls -lh
total 24
-rw-r--r-- 1 shiqiang staff 60B 11 11 19:02 Dockerfile
-rw-r--r-- 1 shiqiang staff 123B 11 11 19:02 Helloworld.java

The content of the code.


public class Helloworld {
 public static void main(String args[]){
 System.out.println("Hello world.\n");
 }
}

With the above preparation, you can edit the contents of Dockerfile.


FROM openjdk:8 							# Based on  openjdk:8  Build a mirror image 
COPY . /usr/src/myapp 			 # Copy the code in the directory into the image  /usr/src/myapp  Location of 
WORKDIR /usr/src/myapp 			# Will  /usr/src/myapp  Set as working directory 
RUN javac Helloworld.java 	 # Run the compile command to compile  Helloworld.java  Program 
CMD ["java", "Helloworld"] # Execute the compiled  Helloworld

Build a mirror file.


$ docker build -t java_in_docker_env_8 . 
$ docker images #  Use  docker images  Command to see the constructed  java_in_docker_env_8  Mirror image 

Run the container.


$ docker run -it --rm java_in_docker_env_8
Hello world.

$ ls -lh     
total 24
-rw-r--r-- 1 shiqiang staff 112B 11 12 10:53 Dockerfile
-rw-r--r-- 1 shiqiang staff 123B 11 12 10:49 Helloworld.java

You can see the output of the program, and see that there are uncompiled results in the current directory. However, this method compiles every time the container is started, and there is no way to share the compilation results. The next section introduces how to save the compilation results outside the container.

Compile and run outside the container

The working directory and code preparation are the same as in the previous section, but the Dockerfile file is slightly different from the previous section.


FROM openjdk:8
COPY . /usr/src/myapp
WORKDIR /usr/src/myapp

Build a mirror file.


$ docker build -t java_env_8
$ docker images #  Use  docker images  Command to see the constructed  java_env_8  Mirror image 

Compile the file.


$ docker run --rm -v "$PWD":/usr/src/myapp -w /usr/src/myapp java_env_8 javac Helloworld.java
$ ls -lh	#  You can see the compiled result 
total 32
-rw-r--r-- 1 shiqiang staff 112B 11 12 10:53 Dockerfile
-rw-r--r-- 1 shiqiang staff 427B 11 12 11:09 Helloworld.class
-rw-r--r-- 1 shiqiang staff 123B 11 12 10:49 Helloworld.java

Run the program.


$ docker run --rm -v "$PWD":/usr/src/myapp -w /usr/src/myapp java_env_8 java Helloworld 
Hello world.

Write at the end

This paper only builds a simple java development environment. The advantage of compiling and running java programs in this way is that there is no need to install jdk locally, and it is more convenient to compile programs for multiple jdk versions. However, complex projects are often built with maven or graddle. Can this project be built in the way mentioned in this article? Please look forward to the next article in this series, and welcome everyone to pay attention to WeChat official account to get the latest article push.

Reference Docker-Create a Java development environment


Related articles: