编程知识 cdmana.com

Using docker to build a simple java development and compilation environment

at present Java There are many versions of the language , Except for the common Java 8, Some legacy projects may use Java 7, There may also be some newer projects that use Java 10 Version above . If you want to switch your local Java development environment , It will take some time to toss it up , And in the future, when switching between different versions, you have to toss about once every time .

Docker It makes it easy for us to maintain different versions of the development and compilation environment , If you don't know what is Docker You can see Docker Introduction . We can build... In two ways java Development environment of , One is to compile and run in a container , One is to compile and run outside the container , Let's take a look at how to operate .

preparation : First make sure that you have installed Docker , If it's convenient to put... In advance openjdk:8 Pull down the mirror image of , It will save everyone's time . In this paper macOs 10.15.7 、Docker 19.03.8 In the environment of .

Compile and run in container

In this paper, a simple Helloworld Program, for example , 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");
    }
}

After all the above preparations have been made , You can edit Dockerfile The content of .

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

Building image files .

$ docker build -t java_in_docker_env_8 . 
$ docker images #  Use  docker images  The command can see the built  java_in_docker_env_8  Mirror image 

Run 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 the current directory and there is not compiled out of the results . But in this way, every time you start the container, you have to compile , And there is no way to share the compilation results , The next section describes how to save the compilation results out of the container .

Compile and run out of container

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

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

Building image files .

$ docker build -t java_env_8
$ docker images #  Use  docker images  The command can see the built  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 result of the compilation 
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.


At the end

This paper only constructs a simple java development environment , Compile and run in this way java The advantage of the program is that there is no need to install it locally jdk , In need of more than one jdk It's more convenient to compile programs with version . But for complex projects maven perhaps graddle Build , Can this kind of project be built in the way described in this article , Look forward to the next article in this series , You are also welcome to receive the latest article push from the official account . If you want to know more about Docker Knowledge about , Please refer to my previous articles .

Reference material

  1. Docker - Create a Java development environment

版权声明
本文为[Cocowool]所创,转载请带上原文链接,感谢

Scroll to Top