资讯专栏INFORMATION COLUMN

使用docker Maven插件本地构建docker镜像并发布到远程服务器

Thanatos / 2541人阅读

摘要:登录网站,生成一个基本的应用。将应用导入并创建类。连接,执行命令,查看可用的镜像执行命令,使用镜像启动容器。至此,使用插件构建镜像并运行容器完成,访问路径,获得结果

1.登录网站https://start.spring.io/,生成一个基本的SpringBoot应用。

2.将应用导入Eclipse IDE并创建Application类。目录结构如下:

Application代码如下:

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.在pom.xml文件中增加插件配置:


            
                com.spotify
                docker-maven-plugin
                0.4.12
                
                    
                    
                    microservice-eureka-demo
                    java
                    ["java", "-jar", "/${project.build.finalName}.jar"]
                    
                        
                            /
                            ${project.build.directory}
                            ${project.build.finalName}.jar
                        
                    
                
            

4.pom.xml文件中增加依赖:

          
            org.springframework.boot  
            spring-boot-starter-web  
        

5.配置本地环境变量,设置docker文件构建主机(主机192.168.244.130已经安装完docker环境)

root@ubuntu-02:~# docker -v
Docker version 17.05.0-ce, build 89658be

本地环境变量配置如下:

6.进入代码目录,执行命令“mvn clean package docker:build”项目编译成功。

[INFO]
[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ dockerdemo ---
[INFO] Building jar: C:UsersSeanDownloadsdemodemo	argetdockerdemo-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:UsersSeanDownloadsdemodemo	argetdockerdemo-0.0.1-SNAPSHOT.jar -> C:UsersSeanDownloadsdemodemo	argetdockerdockerdemo-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连接192.168.244.130,执行命令“docker images”,查看可用的镜像:

root@ubuntu-02:~# docker images
REPOSITORY                                                           TAG                 IMAGE ID            CREATED             SIZE
microservice-eureka-demo                                             latest              1f800bf8cb24        23 minutes ago      658MB

8.执行命令“docker run -p 8080:8080 microservice-eureka-demo:latest”,使用镜像microservice-eureka-demo:latest启动docker容器。

root@ubuntu-02:~# 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.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@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.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@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> 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.执行命令“docker ps”,查看启动的docker容器。

root@ubuntu-02:~# 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.执行命令“docker exec -it 08099bd4b8e1 bin/bash”,进入docker容器。

root@ubuntu-02:~# docker exec -it 08099bd4b8e1 bin/bash 
root@08099bd4b8e1:/# 

11.执行命令“ps -ef”,可用查看到docker容器里面启动的进程,即是pom.xml配置文件中配置的插件docker-maven-plugin的entryPoint,java -jar /dockerdemo-0.0.1-SNAPSHOT.jar。

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

root@08099bd4b8e1:/# 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
root@08099bd4b8e1:/#

12.至此,使用docker插件构建docker镜像并运行docker容器完成,访问路径“http://192.168.244.130:8080/p...”,获得controller结果:

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/67462.html

相关文章

  • 使用docker Maven插件本地构建docker镜像发布远程务器

    摘要:登录网站,生成一个基本的应用。将应用导入并创建类。连接,执行命令,查看可用的镜像执行命令,使用镜像启动容器。至此,使用插件构建镜像并运行容器完成,访问路径,获得结果 1.登录网站https://start.spring.io/,生成一个基本的SpringBoot应用。 2.将应用导入Eclipse IDE并创建Application类。目录结构如下: showImg(https://s...

    jcc 评论0 收藏0
  • Jenkins集成Docker镜像实现自动发布

    摘要:集群环境的部署方式使用集群管理工具通过版本新增文件实现滚动升级容器等同于应用的部署 1. 思路&流程 Jenkins集成Docker镜像实现自动发布与Jenkins发布mavne项目思路一样总体流程 为:Jenkins 拉去远端源码 —— gitl实现应用打包 —— jenkins本地把应用程序打包进Docker镜像 ...

    defcon 评论0 收藏0
  • 解放双手 | Jenkins + gitlab + maven 自动打包部署项目

    摘要:什么是可以简单地理解为可以代替你实现自动从拉取代码,打包,部署到远程服务器的工具。随后登录后发现页面空白,没找到具体原因,重启下容器后再登录就好了。主要是配置访问服务器拉取代码,还有配置远程服务器地址,目的是为了打包后上传文件到远程服务器。 showImg(https://segmentfault.com/img/remote/1460000017907307?w=495&h=300)...

    JouyPub 评论0 收藏0

发表评论

0条评论

Thanatos

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<