Build the docker image locally and publish it to the remote server using the docker Maven plug-in

Time:2020-3-27

1. Log in https://start.spring.io/ to generate a basic spring boot application.

2. Import the application into the eclipse IDE and create the application class. The directory structure is as follows:

Build the docker image locally and publish it to the remote server using the docker Maven plug-in

The application code is as follows:

package com.example.demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;



@RestController
public class Application {
    
    @RequestMapping(value="/phone")
    public String index(){
        return "Hello World!";
    }
}

3. Add plug-in configuration in pom.xml file:

<! -- docker's Maven plug-in, official website: https://github.com/spotify/docker-maven-plugin -- >
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>0.4.12</version>
                <configuration>
                    <! -- note that imagename must be regular [a-z0-9 -], otherwise the build will not succeed -- >
                    <! -- see: https://github.com/spotify/docker-maven-plugin invalid repository name... Only [a-z0-9 -?] are allowed -- >
                    <imageName>microservice-eureka-demo</imageName>
                    <baseImage>java</baseImage>
                    <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <directory>${project.build.directory}</directory>
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>

4. Add dependency in pom.xml file:

        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-web</artifactId>  
        </dependency>

5. Configure the local environment variables and set the docker file building host (the host 192.168.244.130 has installed the docker environment)

[email protected]:~# docker -v
Docker version 17.05.0-ce, build 89658be

The local environment variables are configured as follows:
Build the docker image locally and publish it to the remote server using the docker Maven plug-in

6. Enter the code directory and execute the command “MVN clean package docker: build” to compile successfully.

[INFO]
[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ dockerdemo ---
[INFO] Building jar: C:\Users\Sean\Downloads\demo\demo\target\dockerdemo-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:1.5.4.RELEASE:repackage (default) @ dockerdemo ---
[INFO]
[INFO] --- docker-maven-plugin:0.4.12:build (default-cli) @ dockerdemo ---
[INFO] Copying C:\Users\Sean\Downloads\demo\demo\target\dockerdemo-0.0.1-SNAPSHOT.jar -> C:\Users\Sean\Downloads\demo\demo\target\docker\dockerdemo-0.0.1-SNAPSHOT.jar
[INFO] Building image microservice-eureka-demo
Step 1/3 : FROM java
 ---> d23bdf5b1b1b
Step 2/3 : ADD /dockerdemo-0.0.1-SNAPSHOT.jar //
 ---> e1b3c3de711c
Removing intermediate container a9e57b1d9883
Step 3/3 : ENTRYPOINT java -jar /dockerdemo-0.0.1-SNAPSHOT.jar
 ---> Running in 7a3cdc1d112b
 ---> 1f800bf8cb24
Removing intermediate container 7a3cdc1d112b
Successfully built 1f800bf8cb24
Successfully tagged microservice-eureka-demo:latest
[INFO] Built microservice-eureka-demo
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 39.982 s
[INFO] Finished at: 2017-07-23T21:51:21+08:00
[INFO] Final Memory: 42M/393M
[INFO] ------------------------------------------------------------------------

7. SSH to 192.168.244.130, execute the command “docker images” to view the available images:

[email protected]:~# docker images
REPOSITORY                                                           TAG                 IMAGE ID            CREATED             SIZE
microservice-eureka-demo                                             latest              1f800bf8cb24        23 minutes ago      658MB

8. Execute the command “docker run – P 8080:8080 microservice Eureka Demo: latest”, and start the docker container with the mirror microservice Eureka Demo: latest.

[email protected]:~# docker run -p 8080:8080 microservice-eureka-demo:latest

  .   ____          _            __ _ _
 /\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.4.RELEASE)

2017-07-23 14:19:21.766  INFO 1 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication v0.0.1-SNAPSHOT on 656842f890d3 with PID 1 (/dockerdemo-0.0.1-SNAPSHOT.jar started by root in /)
2017-07-23 14:19:21.792  INFO 1 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
2017-07-23 14:19:22.402  INFO 1 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot[email protected]5a2e4553: startup date [Sun Jul 23 14:19:22 UTC 2017]; root of context hierarchy
2017-07-23 14:19:31.950  INFO 1 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-07-23 14:19:32.015  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2017-07-23 14:19:32.032  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.15
2017-07-23 14:19:32.467  INFO 1 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2017-07-23 14:19:32.467  INFO 1 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 10082 ms
2017-07-23 14:19:33.101  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2017-07-23 14:19:33.141  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-07-23 14:19:33.148  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-07-23 14:19:33.161  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-07-23 14:19:33.161  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2017-07-23 14:19:34.708  INFO 1 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot[email protected]5a2e4553: startup date [Sun Jul 23 14:19:22 UTC 2017]; root of context hierarchy
2017-07-23 14:19:35.105  INFO 1 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/phone]}" onto public java.lang.String com.example.demo.controller.Application.index()
2017-07-23 14:19:35.127  INFO 1 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-07-23 14:19:35.138  INFO 1 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-07-23 14:19:35.277  INFO 1 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-07-23 14:19:35.285  INFO 1 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-07-23 14:19:35.466  INFO 1 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-07-23 14:19:36.180  INFO 1 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-07-23 14:19:36.449  INFO 1 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-07-23 14:19:36.477  INFO 1 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 17.363 seconds (JVM running for 19.474)

9. Execute the command “docker PS” to view the launched docker container.

[email protected]:~# docker ps
CONTAINER ID        IMAGE                             COMMAND                  CREATED             STATUS              PORTS                    NAMES
08099bd4b8e1        microservice-eureka-demo:latest   "java -jar /docker..."   27 seconds ago      Up 26 seconds       0.0.0.0:8080->8080/tcp   upbeat_borg

10. Execute the command “docker exec – it 08099bd4b8e1 bin / bash” to enter the docker container.

[email protected]:~# docker exec -it 08099bd4b8e1 bin/bash 
[email protected]:/# 

11. Execute the command “ps-ef”, and you can view the process started in the docker container, that is, the entrypoint of the plug-in docker Maven plugin configured in the pom.xml configuration file, Java – jar / dockerdemo-0.0.1-snapshot.jar.

<entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>

[email protected]:/# ps -ef
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  4 14:20 ?        00:00:17 java -jar /dockerdemo-0.0.1-SNAPSHOT.jar
root         34      0  0 14:24 ?        00:00:00 bin/bash
root         41     34  0 14:26 ?        00:00:00 ps -ef
[email protected]:/#

12. At this point, use the docker plug-in to build the docker image and run the docker container. Go to the path “http://192.168.244.130:8080 / P…” and get the controller result:

Build the docker image locally and publish it to the remote server using the docker Maven plug-in

Recommended Today

Deeply analyze the principle and practice of RSA key

1、 Preface After experiencing many dark moments in life, when you read this article, you will regret and even be angry: why didn’t you write this article earlier?! Your darkest moments include: 1. Your project needs to be connected with the bank, and the other party needs you to provide an encryption certificate. You have […]