The SpringBoot project is packaged as a Docker image
With the development of microservices, service docker has become a trend. This paper records the whole process of packaging the SpringBoot project into docker.
catalog:
1. Prepare docker private server environment
2. Service docker packaging configuration
3. Service packaging & run validation
4. Docker related instructions
Prepare docker private server environment
Prepare a server or virtual machine as Docker private server warehouse. Refer to [Docker environment configuration] After the environment is ready, open the Docker API port for image upload during service packaging. And basic image preparation.
Open docker private server API port
Add - h to [Service] - [ExecStart] in / usr/lib/systemd/system/docker.service tcp://127.0.0.1:2375 As follows:
$ sudo vi /usr/lib/systemd/system/docker.service
Reload systemctl configuration
$ sudo systemctl daemon-reload
Restart Docker
$ sudo systemctl restart docker.service
Check whether the output confirmed by netstat is dockerd listening on the configured port
$ sudo netstat -lntp | grep dockerd
As shown in the figure, the operation is successful
Download basic image (download on demand)
$ docker pull java:8
Wait until the download is successful. Note that if it is a virtual machine, the memory and disk should not be too small, otherwise there will be problems such as download failure or docker service operation failure.
Service docker packaging configuration
Build a Springboot project to package and configure the Docker image.
The Swagger interface accessed by the local IDEA running service is shown in the figure below:
Add the following configuration to the project pom.xml:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.1.1.RELEASE</version> </plugin> <!-- Docker maven plugin --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- Docker Package upload --> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>1.2.0</version> <executions> <execution> <id>build-image</id> <phase>package</phase> <goals> <goal>build</goal> </goals> </execution> </executions> <configuration> <!--buildArgs be used for Dockerfile In the file --> <buildArgs> <JAR_FILE>${project.build.finalName}.jar</JAR_FILE> </buildArgs> <!-- In step 1 docker Private server address --> <dockerHost>http://192.168.4.56:2375</dockerHost> <!-- imageName Capital letters are not allowed --> <imageName>${project.build.finalName}</imageName> <!--TAG,The project version number is used here,Modify the project when rebuilding the mirror ${project.version} --> <imageTags> <imageTag>${project.version}</imageTag> </imageTags> <!--Dependent base image --> <baseImage>java</baseImage> <!--Dockerfile Location of --> <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory> <!-- Here is replication jar Package to docker Container specified directory configuration --> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> <!-- take forceTags Set as true,This will overwrite the mirror that builds the same label --> <forceTags>true</forceTags> </configuration> </plugin> </plugins> <finalName>${project.artifactId}</finalName> </build>
Build Dockerfile (Note: no file format suffix)
# Basic dependencies required for service operation FROM java:8 # Author information MAINTAINER zp app@xxx.com # Data volume mount address VOLUME /tmp ARG JAR_FILE ADD ${JAR_FILE} app.jar RUN bash -c 'touch /app.jar' #Set time zone RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone # Container run port EXPOSE 8082 # java runtime environment configuration (not required) ENV JAVA_OPTS="\ -server \ -Xms2048m \ -Xmx2048m \ -Xmn512m \ -XX:SurvivorRatio=8 \ -XX:MetaspaceSize=256m \ -XX:MaxMetaspaceSize=256m \ -XX:ParallelGCThreads=4 \ -XX:+PrintGCDetails \ -XX:+PrintTenuringDistribution \ -XX:+PrintGCTimeStamps \ -XX:+HeapDumpOnOutOfMemoryError \ -XX:HeapDumpPath=/ \ -XX:+DisableExplicitGC \ -XX:+UseConcMarkSweepGC \ -XX:+UseParNewGC \ -XX:+CMSParallelRemarkEnabled \ -XX:+CMSClassUnloadingEnabled \ -XX:LargePageSizeInBytes=128M \ -XX:+UseFastAccessorMethods \ -XX:+UseCMSInitiatingOccupancyOnly \ -XX:CMSInitiatingOccupancyFraction=80 \ -XX:SoftRefLRUPolicyMSPerMB=0 \ -XX:+PrintClassHistogram \ -XX:+PrintHeapAtGC \ -XX:+UnlockDiagnosticVMOptions \ -XX:+UnlockExperimentalVMOptions \ -XX:+PrintFlagsFinal \ -XX:GCLogFileSize=10M" #Running docker starts the service ENTRYPOINT java ${JAVA_OPTS} -Djava.security.egd=file:/dev/./urandom -jar /app.jar
Note that the address and resource path in the above configuration need to correspond.
Service packaging & run validation
Perform mvn package build in Terminal in IDEA:
mvn clean install -Dmaven.test.skip=true
Or package the above instructions for CMD operation in the drive letter path of the corresponding project of the computer.
After the packaging instruction is completed, as shown in the figure:
View in docker private server warehouse:
If an image with the same configuration as the service appears, the service Docker image is packaged successfully.
Run the image for service access verification.
Operation instruction:
docker run -it -d --name doc --restart=always -p 8082:8082 documents:1.0
– name doc is the name of the container used for the operation of the image. You can view the relevant logs of the operation through the name. If the operation fails, you can check through the logs:
docker logs doc
The operation is completed as shown in the figure:
Access the service through Docker private server in the browser, and the verification is as shown in the figure:
Since then, the SpringBoot service has packaged the Docker image.
Docker related instructions
1.View all currently running containers docker ps -a 2.Stop all containers( container),Only in this way can you delete the images: docker stop $(docker ps -a -q) 3.If you want to delete all containers( container)If yes, add another instruction: docker rm $(docker ps -a -q) If such deletion fails, it can be adopted docker rmi -f image_ID 4.View the current images( images) docker images