IDEA SpringBoot项目Docker一键部署

1.配置docker远程连接端口

首先编辑我们服务器上的docker文件

vim /usr/lib/systemd/system/docker.service

修改以ExecStart开头的行(centos 7):添加

-H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock \

IDEA SpringBoot项目Docker一键部署_第1张图片 

修改后保存文件,然后重启docker

systemctl daemon-reload

service docker restart

重启之后测试远程连接是否正常,这里的2375是之前配置的端口

curl http://localhost:2375/version

看到返回信息基本上就没有问题了

{"Version":"1.13.1","ApiVersion":"1.26","MinAPIVersion":"1.12","GitCommit":"7f2769b/1.13.1","GoVersion":"go1.10.3","Os":"linux","Arch":"amd64","KernelVersion":"3.10.0-957.21.3.el7.x86_64","BuildTime":"2019-09-15T14:06:47.565778468+00:00","PkgVersion":"docker-1.13.1-103.git7f2769b.el7.centos.x86_64"}

然后开启端口,或者关闭防火墙,二者选其一即可

firewall-cmd --zone=public --add-port=2375/tcp --permanent  
chkconfig iptables off

 

2.使用idea连接到docker

首先下载docker插件,idea2019自带了docker插件。如果没有插件可以选择安装docker插件IDEA SpringBoot项目Docker一键部署_第2张图片

然后配置docker地址,在你的File | Settings | Build, Execution, Deployment | DockerIDEA SpringBoot项目Docker一键部署_第3张图片配置完成链接之后,出现了框中的内容即可.IDEA SpringBoot项目Docker一键部署_第4张图片链接成功之后会列出容器和镜像!

配置阿里云镜像加速器:IDEA SpringBoot项目Docker一键部署_第5张图片

3.docker-maven-plugin 介绍

在我们持续集成过程中,项目工程一般使用 Maven 编译打包,然后生成镜像,通过镜像上线,能够大大提供上线效率,同时能够快速动态扩容,快速回滚,着实很方便。docker-maven-plugin 插件就是为了帮助我们在Maven工程中,通过简单的配置,自动生成镜像并推送到仓库中。

pom.xml:


 
        ${project.artifactId}
 
        
 
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    true
                
            
            
            
                org.apache.maven.plugins
                maven-surefire-plugin
                
                    true
                
            
 
            
            
                com.spotify
                docker-maven-plugin
                1.0.0
                
                
                    
                        build-image
                        
                        package
                        
                            build
                        
                    
                
                
                    
                    bruceliu/${project.artifactId}
                    
                    
                        latest
                    
                    
                    java
                    
                    [email protected]
                    
                    /ROOT
                    ["java", "-version"]
                    ["java", "-jar", "/${project.build.finalName}.jar"]                    
                    
                    http://122.51.50.249:2375
                    
                    
                        
                            /
                            
                            ${project.build.directory}
                            
                            ${project.build.finalName}.jar
                        
                    
                
            
 
        
    

IDEA执行maven打包命令:

IDEA SpringBoot项目Docker一键部署_第6张图片

 

G:\softDevelopment\JDK8\bin\java -Dmaven.multiModuleProjectDirectory=E:\workspace2017\elk-web -Dmaven.home=E:\Maven20190910\apache-maven-3.6.1 -Dclassworlds.conf=E:\Maven20190910\apache-maven-3.6.1\bin\m2.conf "-javaagent:G:\idea2017\IntelliJ IDEA 2017.3.1\lib\idea_rt.jar=49260:G:\idea2017\IntelliJ IDEA 2017.3.1\bin" -Dfile.encoding=UTF-8 -classpath E:\Maven20190910\apache-maven-3.6.1\boot\plexus-classworlds-2.6.0.jar org.codehaus.classworlds.Launcher -Didea.version=2017.3.7 -s E:\Maven20190910\apache-maven-3.6.1\conf\settings.xml -Dmaven.repo.local=E:\Maven20190910\repository package
[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for com.bruceliu.elk.demo:elk-web:jar:1.0-SNAPSHOT
[WARNING] 'build.plugins.plugin.(groupId:artifactId)' must be unique but found duplicate declaration of plugin org.springframework.boot:spring-boot-maven-plugin @ line 36, column 21
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO] 
[INFO] -------------------< com.bruceliu.elk.demo:elk-web >--------------------
[INFO] Building elk-web 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ elk-web ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ elk-web ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to E:\workspace2017\elk-web\target\classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ elk-web ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:\workspace2017\elk-web\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ elk-web ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ elk-web ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ elk-web ---
[INFO] Building jar: E:\workspace2017\elk-web\target\elk-web.jar
[INFO] 
[INFO] --- spring-boot-maven-plugin:1.5.4.RELEASE:repackage (default) @ elk-web ---
[INFO] 
[INFO] --- docker-maven-plugin:1.0.0:build (build-image) @ elk-web ---
[INFO] Using authentication suppliers: [ConfigFileRegistryAuthSupplier]
[INFO] Copying E:\workspace2017\elk-web\target\elk-web.jar -> E:\workspace2017\elk-web\target\docker\elk-web.jar
[INFO] Building image bruceliu/elk-web
Step 1/6 : FROM java
 ---> d23bdf5b1b1b
Step 2/6 : MAINTAINER [email protected]
 ---> Running in 787e4786fbd4
 ---> 4d4519f52fda
Removing intermediate container 787e4786fbd4
Step 3/6 : WORKDIR /ROOT
 ---> f40dcbc9a9eb
Removing intermediate container 7fa6bbc9d1df
Step 4/6 : ADD /elk-web.jar //
 ---> c7f1107ae3d4
Removing intermediate container f370558f1a38
Step 5/6 : ENTRYPOINT java -jar /elk-web.jar
 ---> Running in e4480ced0829
 ---> b634ca5fa5ad
Removing intermediate container e4480ced0829
Step 6/6 : CMD java -version
 ---> Running in cc6a064ef921
 ---> cf9a5d50326b
Removing intermediate container cc6a064ef921
Successfully built cf9a5d50326b
[INFO] Built bruceliu/elk-web
[INFO] Tagging bruceliu/elk-web with latest
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  10.329 s
[INFO] Finished at: 2019-12-29T22:44:06+08:00
[INFO] ------------------------------------------------------------------------
 
Process finished with exit code 0

你可能感兴趣的:(docker,spring,boot,一键部署)