Implementation method of deploying Spring Boot using Docker
The development of Docker technology provides a more convenient environment for the landing of micro-services. It is actually very easy to deploy Spring Boot using Docker. Let's learn about this article.
First build a simple Spring Boot project, then add Docker support to the project, and finally deploy the project.
A simple Spring Boot project
In pom.xml, use Spring Boot 2.0 related dependencies
Org.springframework.boot spring-boot-starter-parent 2.0.0.RELEASE
Add web and test dependencies
Org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test
Create a DockerController with an index () method that returns: Hello Docker!
@ RestControllerpublic class DockerController {@ RequestMapping ("/") public String index () {return "Hello Docker!";}}
Startup class
@ SpringBootApplicationpublic class DockerApplication {public static void main (String [] args) {SpringApplication.run (DockerApplication.class, args);}}
After adding, start the project. After starting successfully, the browser asks: http://localhost:8080/, the page returns: Hello docking, indicating that the configuration of the Spring Boot project is normal.
Add Docker support for Spring Boot project
Add the Docker image name to the pom.xml-properties
Springboot
Add the Docker build plug-in to plugins:
Org.springframework.boot spring-boot-maven-plugin com.spotify docker-maven-plugin 1.0.0 ${docker.image.prefix} / ${project.artifactId} src/main/docker / ${project.build.directory} ${project.build.finalName} .jar
Create a Dockerfile file under the directory src/main/docker, and the Dockerfile file is used to explain how to build the image.
FROM openjdk:8-jdk-alpineVOLUME / tmpADD spring-boot-docker-1.0.jar app.jarENTRYPOINT ["java", "- Djava.security.egd=file:/dev/./urandom", "- jar", "/ app.jar"]
The Dockerfile file is very simple. Build the Jdk basic environment, add Spring Boot Jar to the image, and briefly explain:
FROM means to use the Jdk8 environment as the basic image. If the image is not local, VOLUME will be downloaded from DockerHub. VOLUME points to a directory of / tmp. Because Spring Boot uses the built-in Tomcat container, Tomcat uses / tmp as the working directory by default. The effect of this command is to create a temporary file under the / var/lib/docker directory of the host and link it to the / tmp directory ADD in the container, copy the file and rename ENTRYPOINT. To shorten the startup time of Tomcat, add the system attribute of java.security.egd pointing to / dev/urandom as ENTRYPOINT
This completes adding Docker dependencies to the Spring Boot project.
Build a packaging environment
We need a Docker environment to package the Spring Boot project, and it's troublesome to build a Docker environment in Windows, so I'll take Centos 7 as an example.
Install the Docker environment
Installation
Yum install docker
After the installation is complete, use the following command to start the docker service and set it to boot:
Service docker startchkconfig docker on#LCTT translation note: the old sysv syntax is used here, such as the new systemd syntax supported in CentOS 7, as follows: systemctl start docker.servicesystemctl enable docker.service
Use Docker China Accelerator
After vi / etc/docker/daemon.json# is added: {"registry-mirrors": ["https://registry.docker-cn.com"]," live-restore ": true}
Restart docker
Systemctl restart docker
Enter docker version to return version information and the installation is normal.
Install JDK
Yum-y install java-1.8.0-openjdk*
Configure the environment variable and open vim / etc/profile to add something.
Export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.161-0.b14.el7_4.x86_64 export PATH=$PATH:$JAVA_HOME/bin
After the modification is completed, make it effective
Source / etc/profile
Enter java-version to return the version information and the installation is normal.
Install MAVEN
Download: http://mirrors.shu.edu.cn/apache/maven/maven-3/3.5.2/binaries/apache-maven-3.5.2-bin.tar.gz
# # decompressing tar vxf apache-maven-3.5.2-bin.tar.gz## Mobile mv apache-maven-3.5.2 / usr/local/maven3
Modify the environment variable to add the following lines in / etc/profile
MAVEN_HOME=/usr/local/maven3export MAVEN_HOMEexport PATH=$ {PATH}: ${MAVEN_HOME} / bin
Remember to execute source / etc/profile to make the environment variable take effect.
Enter mvn-version to return the version information and the installation is normal.
In this way, the entire build environment is configured.
Deploy a Spring Boot project using Docker
Put the project spring-boot-docker in the copy server and go to the project path for packaging and testing.
# package mvn package# to start java-jar target/spring-boot-docker-1.0.jar
Seeing the startup log of Spring Boot shows that there is nothing wrong with the environment configuration, so let's use DockerFile to build the image.
Mvn package docker:build
The first build may be a bit slow, which indicates a successful build when you see the following:
.. Step 1: FROM openjdk:8-jdk-alpine-- > 224765a6bdbeStep 2: VOLUME / tmp-- > Using cache-- > b4e86cc8654eStep 3: ADD spring-boot-docker-1.0.jar app.jar-- > a20fe75963abRemoving intermediate container 593ee5e1ea51Step 4: ENTRYPOINT java-Djava.security.egd=file:/dev/./urandom-jar / app.jar-- > Running in 85d558a10cd4-> 7102f08b5e95Removing intermediate container 85d558a10cd4Successfully built 7102f08b5e95 [INFO] Built springboot/spring-boot-docker [INFO]- -[INFO] BUILD SUCCESS [INFO]- [INFO] Total time: 54.346 s [INFO] Finished at: 2018-03-13T16:20:15+08:00 [INFO] Final Memory: 42M/182M [INFO]-- -
Use the docker images command to view the built image:
Docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEspringboot/spring-boot-docker latest 99ce9468da74 6 seconds ago 117.5 MB
Springboot/spring-boot-docker is the image we have built. The next step is to run the image.
Docker run-p 8080 8080-t springboot/spring-boot-docker
After the startup is complete, we use docker ps to view the running images:
Docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES049570da86a9 springboot/spring-boot-docker "java-Djava.security" 30 seconds ago Up 27 seconds 0.0.0.0 Djava.security 8080-> 8080/tcp determined_mahavira
You can see that the container we built is running. Visit the browser: http://192.168.0.x:8080/, returns
Hello Docker!
It indicates that using Docker to deploy the Spring Boot project is successful!
The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.