Spring boot is packaged and deployed with docker

1. Add dependencies

Introduce dockerfile-maven-plugin component

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>dockerfile-maven-plugin</artifactId>
                <version>1.4.13</version>
                <executions>
                    <execution>
                        <id>default</id>
                        <goals>
                            <goal>build</goal>
                            <goal>push</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <repository>${docker.image.prefix}/${project.artifactId}</repository>
                    <tag>${project.version}</tag>
                    <buildArgs>
                        <JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
                    </buildArgs>
                </configuration>
            </plugin>
        </plugins>
</build>

2. Add the file Dockerfile

  Dockerfile and pom.xml are in the same level directory

FROM java:8
EXPOSE 8080
ARG JAR_FILE
ADD target/${JAR_FILE} /demo-0.0.jar
ENTRYPOINT ["java", "-jar","/demo-0.0.jar"]

3. Pack

>mvn package -DskipTests
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< com.example:demo >--------------------------
[INFO] Building demo 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 7 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 14 source files to E:\java\demo\target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:\java\demo\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to E:\java\demo\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ demo ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:3.1.2:jar (default-jar) @ demo ---
[INFO] Building jar: E:\java\demo\target\demo-1.0.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.2.5.RELEASE:repackage (repackage) @ demo ---
[INFO] Replacing main artifact with repackaged archive
[INFO]
[INFO] --- dockerfile-maven-plugin:1.4.13:build (default) @ demo ---
[INFO] dockerfile: null
[INFO] contextDirectory: E:\java\demo
[INFO] Building Docker context E:\java\demo
[INFO] Path(dockerfile): null
[INFO] Path(contextDirectory): E:\java\demo
[INFO]
[INFO] Image will be built as springdemo/demo:1.0
[INFO]
[INFO] Step 1/5 : FROM java:8
[INFO]
[INFO] Pulling from library/java
[INFO] Digest: sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d
[INFO] Status: Image is up to date for java:8
[INFO]  ---> d23bdf5b1b1b
[INFO] Step 2/5 : EXPOSE 8080
[INFO]
[INFO]  ---> Using cache
[INFO]  ---> 8b6cab01e7f6
[INFO] Step 3/5 : ARG JAR_FILE
[INFO]
[INFO]  ---> Using cache
[INFO]  ---> 7ba55f2e8bba
[INFO] Step 4/5 : ADD target/${JAR_FILE} /demo-0.0.jar
[INFO]
[INFO]  ---> 7263886dec7b
[INFO] Step 5/5 : ENTRYPOINT ["java", "-jar","/demo-0.0.jar"]
[INFO]
[INFO]  ---> Running in 12b670b157c4
[INFO] Removing intermediate container 12b670b157c4
[INFO]  ---> 172f4efb3acd
[INFO] Successfully built 172f4efb3acd
[INFO] Successfully tagged springdemo/demo:1.0
[INFO]
[INFO] Detected build of image with id 172f4efb3acd
[INFO] Building jar: E:\java\demo\target\demo-1.0-docker-info.jar
[INFO] Successfully built springdemo/demo:1.0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ----------------------------------------------- ------------------------- 
[INFO] Total time: 01:18 min
[INFO] Finished at: 2020-04-16T09:04:50+08:00
[INFO] ------------------------------------------------------------------------

Description:

  After the command is executed, the image file will be on the server

  java: 8 is a mirror-dependent layer, if it does not exist, it will be pulled down when the mvn dockerfile: build command is executed

4. Deploy

Log in to the docker server

sudo docker images

 

run

docker run --name springdemo -p 8080:8080 -d  springdemo/demo:1.0

Note:

  The version number is not last, you need to specify the version number 

test

GET http://192.168.99.100:8080/hello

return

  Hello World!

 

Guess you like

Origin www.cnblogs.com/baby123/p/12707154.html