Docker deploys SpringBook project

Keywords: Docker Maven Java SpringBoot

Deploy springboot project on docker

First, add the Docker image name to the pom.xml file of the springboot project:

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

Add docker to plugin to build plug-ins:

<build>
 <plugins>
  <plugin>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-maven-plugin</artifactId>
  </plugin>
  <!-- Docker maven plugin -->
  <plugin>
   <groupId>com.spotify</groupId>
   <artifactId>docker-maven-plugin</artifactId>
   <version>1.0.0</version>
   <configuration>
    <imageName>${docker.image.prefix}/${project.artifactId}</imageName>
    <dockerDirectory>src/main/docker</dockerDirectory>
    <resources>
     <resource>
      <targetPath>/</targetPath>
      <directory>${project.build.directory}</directory>
      <include>${project.build.finalName}.jar</include>
     </resource>
    </resources>
   </configuration>
  </plugin>
  <!-- Docker maven plugin -->
 </plugins>
</build>

Use maven to punch jar bags and throw them on centos.
vim Creates Dockerfile

FROM java:8
# FROM: Represents the underlying image, the runtime environment
VOLUME /tmp
# VOLUME: A specially specified directory for storing data. The function of this command is to create a directory named tmp in / var/lib/docker. When opening redis service, you need to specify which folder redis data is stored in. This command is very useful at this time.
ADD docker-0.0.1-SNAPSHOT.jar app.jar 
# ADD: Copy files and rename them
EXPOSE 8080
# EXPOSE: It's not really a release port. It's just a communication between the container deployer and the person who created the image, that is, the person who created the image tells the container deployer which port the container should map to the outside world.
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
# ENTRYPOINT: The command that runs when the container starts is equivalent to entering java -jar xxxx.jar on the command line. To shorten the starting time of Tomcat, add the system attribute of java.security.egd to point to / dev/urandom as ENTRYPOINT.

Create a folder called docker in centos and throw both the jar package and the Dockerfile in it
Enter the folder and run the command: Build the project as an image

docker build -t docker  .

View the details of the mirror and run the command:

docker images

Run the mirror and execute the command:

docker run -p 8080:8080 -d docker

- p represents port mapping, colon left is port number outside container (exposed), right is port number inside container (port number of project), - d is background running, docker is the value of REPOSITORY in the result of command docker images

View the running image and run the command:

docker ps

END

Posted by yjanni on Fri, 04 Oct 2019 00:36:51 -0700