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 \
修改后保存文件,然后重启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插件
然后配置docker地址,在你的File | Settings | Build, Execution, Deployment | Docker配置完成链接之后,出现了框中的内容即可.链接成功之后会列出容器和镜像!
配置阿里云镜像加速器:
3.docker-maven-plugin 介绍
在我们持续集成过程中,项目工程一般使用 Maven 编译打包,然后生成镜像,通过镜像上线,能够大大提供上线效率,同时能够快速动态扩容,快速回滚,着实很方便。docker-maven-plugin 插件就是为了帮助我们在Maven工程中,通过简单的配置,自动生成镜像并推送到仓库中。
pom.xml:
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
<!-- 跳过单元测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<!--使用docker-maven-plugin插件-->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<!--将插件绑定在某个phase执行-->
<executions>
<execution>
<id>build-image</id>
<!--用户只需执行mvn package ,就会自动执行mvn docker:build-->
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<!--指定生成的镜像名-->
<imageName>bruceliu/${project.artifactId}</imageName>
<!--指定标签-->
<imageTags>
<imageTag>latest</imageTag>
</imageTags>
<!--指定基础镜像jdk1.8-->
<baseImage>java</baseImage>
<!--镜像制作人本人信息-->
<maintainer>bruceliu@email.com</maintainer>
<!--切换到ROOT目录-->
<workdir>/ROOT</workdir>
<cmd>["java", "-version"]</cmd>
<entryPoint>["java", "-jar", "/${project.build.finalName}.jar"] </entryPoint>
<!--指定远程 docker api地址-->
<dockerHost>http://122.51.50.249:2375</dockerHost>
<!-- 这里是复制 jar 包到 docker 容器指定目录配置 -->
<resources>
<resource>
<targetPath>/</targetPath>
<!--jar 包所在的路径 此处配置的 即对应 target 目录-->
<directory>${project.build.directory}</directory>
<!--用于指定需要复制的文件 需要包含的 jar包 ,这里对应的是 Dockerfile中添加的文件名 -->
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
IDEA执行maven打包命令:
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 bruceliu@email.com
---> 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