The development of Docker technology provides a more convenient environment for micro-service landing. It is actually very simple to deploy Spring Boot with Docker. In this article, we will simply learn.

Start by building 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, Spring Boot 2.0-related dependencies are used


     
  1. <parent>

  2.  <groupId>org.springframework.boot</groupId>

  3.  <artifactId>spring-boot-starter-parent</artifactId>

  4. . < version > 2.0.0 RELEASE < / version >

  5. </parent>

Copy the code

Add Web and test dependencies


     
  1. <dependencies>

  2.  <dependency>

  3.  <groupId>org.springframework.boot</groupId>

  4.  <artifactId>spring-boot-starter-web</artifactId>

  5.  </dependency>

  6.  <dependency>

  7.  <groupId>org.springframework.boot</groupId>

  8.  <artifactId>spring-boot-starter-test</artifactId>

  9.  <scope>test</scope>

  10.  </dependency>

  11. </dependencies>

Copy the code

Create a DockerController with an index() method that returns Hello Docker!


     
  1. @RestController

  2. public class DockerController {

  3.  @RequestMapping("/")

  4.  public String index() {

  5. return "Hello Docker!" ;

  6.  }

  7. }

Copy the code

Start the class


     
  1. @SpringBootApplication

  2. public class DockerApplication {

  3.  public static void main(String[] args) {

  4.  SpringApplication.run(DockerApplication.class, args);

  5.  }

  6. }

Copy the code

After the addition, start the project. After the successful startup, the browser will ask: http://localhost:8080/, and the page will return: Hello Docker! “Indicates that the Spring Boot project is correctly configured.

Spring Boot project adds Docker support

Add the Docker image name to pom.xml-properties


     
  1. <properties>

  2.  <docker.image.prefix>springboot</docker.image.prefix>

  3. </properties>

Copy the code

Add Docker build plugins to plugins:


     
  1. <build>

  2.  <plugins>

  3.  <plugin>

  4.  <groupId>org.springframework.boot</groupId>

  5.  <artifactId>spring-boot-maven-plugin</artifactId>

  6.  </plugin>

  7. <! -- Docker maven plugin -->

  8.  <plugin>

  9.  <groupId>com.spotify</groupId>

  10.  <artifactId>docker-maven-plugin</artifactId>

  11. The < version > 1.0.0 < / version >

  12.  <configuration>

  13.  <imageName>${docker.image.prefix}/${project.artifactId}</imageName>

  14.  <dockerDirectory>src/main/docker</dockerDirectory>

  15.  <resources>

  16.  <resource>

  17.  <targetPath>/</targetPath>

  18.  <directory>${project.build.directory}</directory>

  19.  <include>${project.build.finalName}.jar</include>

  20.  </resource>

  21.  </resources>

  22.  </configuration>

  23.  </plugin>

  24. <! -- Docker maven plugin -->

  25.  </plugins>

  26. </build>

Copy the code

Create a Dockerfile file under the directory SRC /main/docker. The Dockerfile file tells you how to build the image.


     
  1. FROM openjdk:8-jdk-alpine

  2. VOLUME /tmp

  3. ADD spring - the boot - docker - 1.0. Jar app. The jar

  4. ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

Copy the code

Build the Jdk base environment and add the Spring Boot Jar to the image.

  • FROM: indicates that the Jdk8 environment is used as the base image. If the image is not local, it will be downloaded FROM DockerHub

  • VOLUME, VOLUME points to a directory named/TMP. Since Spring Boot uses the built-in Tomcat container, Tomcat uses/TMP as its working directory by default. Create a temporary file in the /var/lib/docker directory on the host and link it to the/TMP directory in the container

  • ADD, copy and rename the file

  • Egd system attribute pointing to /dev/urandom as ENTRYPOINT to shorten Tomcat startup time

This completes the addition of the Docker dependency to the Spring Boot project.

Build a packaging environment

We need a Docker environment to package the Spring Boot project. It is very troublesome to build a Docker environment on Windows, so I take Centos 7 as an example.

Install the Docker environment

The installation


     
  1. yum install docker

Copy the code

Once the installation is complete, use the following command to start the Docker service and set it to boot:


     
  1. ervice docker start

  2. chkconfig docker on

  3. CentOS 7 supports the new systemd syntax, which is as follows:

  4. systemctl start docker.service

  5. systemctl enable docker.service

Copy the code

Use the Docker China accelerator


     
  1. vi /etc/docker/daemon.json

  2. # after adding:

  3. {

  4.  "registry-mirrors": ["https://registry.docker-cn.com"],

  5.  "live-restore": true

  6. }

Copy the code

Restart Docker


     
  1. systemctl restart docker

Copy the code

If docker version is displayed, the installation is normal.

Install the JDK


     
  1. Yum -y install Java -- 1.8.0 comes with its *

Copy the code

Configure environment variables open vim /etc/profile add some content

                                
     
  1. Export JAVA_HOME = / usr/lib/JVM/Java -- 1.8.0 comes with its 1.8.0.161-0. B14. El7_4. X86_64

  2. export PATH=$PATH:$JAVA_HOME/bin

Copy the code

After the modification is complete, make the modification take effect


     
  1. source /etc/profile

Copy the code

Run the Java -version command to return the version information.

Install MAVEN

Download: http://mirrors.shu.edu.cn/apache/maven/maven-3/3.5.2/binaries/apache-maven-3.5.2-bin.tar.gz

                                        
     
  1. # # decompression

  2. The tar VXF apache maven -- 3.5.2 - bin. Tar. Gz

  3. # # mobile

  4. The mv apache maven -- 3.5.2 / usr/local/maven3

Copy the code

Modify the environment variable by adding the following lines to /etc/profile

                                            
     
  1. MAVEN_HOME=/usr/local/maven3

  2. export MAVEN_HOME

  3. export PATH=${PATH}:${MAVEN_HOME}/bin

Copy the code

Remember to execute source /etc/profile for the environment variables to take effect.

Run the MVN -version command to return the version information.

The entire build environment is now configured.

Deploy the Spring Boot project using Docker

Copy the project spring-boot-Docker server into the project path for packaging test.

                                                        
     
  1. # packaged

  2. mvn package

  3. # start

  4. Java - jar target/spring - the boot - docker - 1.0. The jar

Copy the code

After seeing the Boot log of Spring Boot, the environment configuration is ok. Next, we use DockerFile to build the image.


     
  1. mvn package docker:build

Copy the code

The first build may be a bit slow, but it is successful when you see the following:


     
  1. .

  2. Step 1 : FROM openjdk:8-jdk-alpine

  3.  ---> 224765a6bdbe

  4. Step 2 : VOLUME /tmp

  5.  ---> Using cache

  6.  ---> b4e86cc8654e

  7. ADD spring-boot-docker-1.0.jar app.jar

  8.  ---> a20fe75963ab

  9. Removing intermediate container 593ee5e1ea51

  10. Step 4 : ENTRYPOINT java -Djava.security.egd=file:/dev/./urandom -jar /app.jar

  11.  ---> Running in 85d558a10cd4

  12.  ---> 7102f08b5e95

  13. Removing intermediate container 85d558a10cd4

  14. Successfully built 7102f08b5e95

  15. [INFO] Built springboot/spring-boot-docker

  16. [INFO] ------------------------------------------------------------------------

  17. [INFO] BUILD SUCCESS

  18. [INFO] ------------------------------------------------------------------------

  19. [INFO] Total time: 54.346s

  20. [INFO] Finished at: 2018-03-13T16:20:15+08:00

  21. [INFO] Final Memory: 42M/182M

  22. [INFO] ------------------------------------------------------------------------

Copy the code

Use the docker images command to view the built images:

                                                            
     
  1. docker images

  2. REPOSITORY TAG IMAGE ID CREATED SIZE

  3. Springboot /spring-boot-docker latest 99ce9468DA74 6 seconds ago 117.5 MB

Copy the code

Springboot /spring-boot-docker is the image we built and the next step is to run it

                                                                
     
  1. docker run -p 8080:8080 -t springboot/spring-boot-docker

Copy the code

After the startup is complete, we use Docker PS to view the running image:

                                                                    
     
  1. docker ps

  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

  3. 049570DA86a9 Springboot /spring-boot-docker "java-djava. security" 30 seconds ago Up 27 seconds 0.0.0.0:8080->8080/ TCP determined_mahavira

Copy the code

Can see that we build the container is running, access to a browser: http://192.168.0.x:8080/, to return

                                                                        
     
  1. Hello Docker!

Copy the code

Successful deployment of the Spring Boot project using Docker!

The sample code: https://github.com/ityouknow/spring-cloud-examples

END

Find a thousand hardcore readers