使用Docker Maven 插件进行镜像的创建以及上传至私服
1.在进行服务容器化部署的时候,需要将服务以及其运行的环境整个打包做成一个镜像,打包的过程有两种办法,第一种是首选通过maven打成jar包,然后再编写dockerfile,执行docker build 手动构建docker镜像,然后再上传至仓库。第二种是使用docker maven 插件,它可以模拟上述过程,将手动编写dockerfile 的过程使用插件完成,并上传至私服。
2.首先,打开docker 的远程操作,docker 默认是关闭的。
编辑宿主机的 /lib/systemd/system/docker.service 文件,将ExecStart 开头的一行修改为:
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
然后分别重载配置,重启服务
systemctl daemon-reload
service docker restart
接下来访问宿主机的2375端口即可显示如下:
ip:2375/version
{"Platform":{"Name":"Docker Engine - Community"},"Components":[{"Name":"Engine","Version":"19.03.4","Details":{"ApiVersion":"1.40","Arch":"amd64","BuildTime":"2019-10-18T15:50:54.000000000+00:00","Experimental":"false","GitCommit":"9013bf583a","GoVersion":"go1.12.10","KernelVersion":"3.10.0-1062.el7.x86_64","MinAPIVersion":"1.12","Os":"linux"}},{"Name":"containerd","Version":"1.2.10","Details":{"GitCommit":"b34a5c8af56e510852c35414db4c1f4fa6172339"}},{"Name":"runc","Version":"1.0.0-rc8+dev","Details":{"GitCommit":"3e425f80a8c931f88e6d94a8c831b9d5aa481657"}},{"Name":"docker-init","Version":"0.18.0","Details":{"GitCommit":"fec3683"}}],"Version":"19.03.4","ApiVersion":"1.40","MinAPIVersion":"1.12","GitCommit":"9013bf583a","GoVersion":"go1.12.10","Os":"linux","Arch":"amd64","KernelVersion":"3.10.0-1062.el7.x86_64","BuildTime":"2019-10-18T15:50:54.000000000+00:00"}
3.安装私服。docker 的私服是企业将自己开发的应用打包成镜像放到自己的仓库中,区别于docker hub。概念类似于maven 的私服或者gitlab。私服也是容器化的服务,直接拉镜像运行即可。
首先拉取registry镜像,docker pull registry
tomcat latest 882487b8be1d 7 weeks ago 507MB
nginx latest 5a9061639d0a 7 weeks ago 126MB
redis latest de25a81a5a0b 7 weeks ago 98.2MB
portainer/portainer latest 4cda95efb0e4 8 weeks ago 80.6MB
registry latest f32a97de94e1 9 months ago 25.8MB
然后运行,docker run -di --name=registry -p 5000:5000 --restart=always registry
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c826962e4379 registry "/entrypoint.sh /etc…" 4 hours ago Up About an hour 0.0.0.0:5000->5000/tcp registry
c4f8db3bbc83 aac681e42c42 "/bin/sh -c 'java -j…" 3 weeks ago Exited (143) 3 hours ago eureka-server
最后修改配置,让docker 信任私服。进入/etc/docker/daemon.json 将内容修改为:
{
"registry-mirrors": ["https://****.mirror.aliyuncs.com"],
"insecure-registries":["192.168.59.133:5000"]
}
上面的 registry-mirrors 是我自己配置的阿里云的加速地址,可以忽略,下面的 insecure-registries 配置的私服的ip:port,修改完以后执行:
systemctl daemon-reload
service docker restart
重载配置重启服务。重启服务以后记得将registry重新运行,或者可以在启动容器时 通过--restart 指定重启策略,指定为always 即可。随后访问 宿主机的 ip:port/v2/_catalog 可以看到:
{"repositories":[]}
4.使用docker maven 插件
在要打包的工程pom文件中添加docker maven 插件
<build>
<finalName>app</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.13</version>
<configuration>
<imageName>192.168.59.133:5000/${project.artifactId}:${project.version}</imageName>
<baseImage>jdk1.8:1.0</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>
<dockerHost>http://192.168.59.133:2375</dockerHost>
</configuration>
</plugin>
</plugins>
</build>
需要说明的是以下几点:
(1)imageName 指定生成的镜像名称,前面的ip:port 指定的是私服的地址,后面是名称和tag
192.168.59.133:5000/${project.artifactId}:${project.version}
(2)baseImage 指定基础镜像名称,相当于dockerfile 中的FROM ,jdk1.8是我自己构建的一个安装了jdk1.8的镜像
jdk1.8 1.0 b834c0fcc837 4 hours ago 585MB
mycentos 0.1 aac681e42c42 3 weeks ago 655MB
tomcat latest 882487b8be1d 7 weeks ago 507MB
nginx latest 5a9061639d0a 7 weeks ago 126MB
(3)dockerHost 指定安装docker 服务的宿主机的ip 和port
5.执行打包
在控制台执行
mvn clean package docker:build -Dmaven.test.skip=true -DpushImage
跳过单元测试,并同时将镜像上传至私服。可以看到将打印出以下日志:
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< com.java:design-parttern >----------------------
[INFO] Building design-parttern 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ design-parttern ---
[INFO] Deleting C:\common\design-parttern\target
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ design-parttern ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ design-parttern ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 45 source files to C:\common\design-parttern\target\classes
[INFO] /C:/common/design-parttern/src/main/java/com/java/dp/factory/FactoryMain.java: C:\common\design-parttern\src\main\java\com\java\dp\factory\FactoryMain.java使用了未经检查或不安全的操作。
[INFO] /C:/common/design-parttern/src/main/java/com/java/dp/factory/FactoryMain.java: 有关详细信息, 请使用 -Xlint:unchecked 重新编译。
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ design-parttern ---
[INFO] Not copying test resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ design-parttern ---
[INFO] Not compiling test sources
[INFO]
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ design-parttern ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:3.1.2:jar (default-jar) @ design-parttern ---
[INFO] Building jar: C:\common\design-parttern\target\app.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.2.1.RELEASE:repackage (repackage) @ design-parttern ---
[INFO] Replacing main artifact with repackaged archive
[INFO]
[INFO] --- docker-maven-plugin:0.4.13:build (default-cli) @ design-parttern ---
[INFO] Copying C:\common\design-parttern\target\app.jar -> C:\common\design-parttern\target\docker\app.jar
[INFO] Building image 192.168.59.133:5000/design-parttern:0.0.1-SNAPSHOT
Step 1/3 : FROM jdk1.8:1.0
---> b834c0fcc837
Step 2/3 : ADD /app.jar //
---> fcfc87345066
Step 3/3 : ENTRYPOINT ["java", "‐jar","/app.jar"]
---> Running in d11496ba57df
Removing intermediate container d11496ba57df
---> 9c13f6795943
ProgressMessage{id=null, status=null, stream=null, error=null, progress=null, progressDetail=null}
Successfully built 9c13f6795943
Successfully tagged 192.168.59.133:5000/design-parttern:0.0.1-SNAPSHOT
[INFO] Built 192.168.59.133:5000/design-parttern:0.0.1-SNAPSHOT
[INFO] Pushing 192.168.59.133:5000/design-parttern:0.0.1-SNAPSHOT
The push refers to repository [192.168.59.133:5000/design-parttern]
f84c21ed068b: Pushed
a52095cbb8d6: Pushed
8b67330a55d1: Pushed
9e607bb861a7: Pushed
0.0.1-SNAPSHOT: digest: sha256:3ec3d50af6c47400513ba8a5d574526408cad3b117057ea85753960c04505042 size: 1166
null: null
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 29.419 s
[INFO] Finished at: 2019-12-09T17:42:56+08:00
[INFO] ------------------------------------------------------------------------
查看docker 镜像发现我们的宿主机上已经有了镜像,docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.59.133:5000/design-parttern 0.0.1-SNAPSHOT 9c13f6795943 2 hours ago 605MB
jdk1.8 1.0 b834c0fcc837 4 hours ago 585MB
mycentos 0.1 aac681e42c42 3 weeks ago 655MB
tomcat latest 882487b8be1d 7 weeks ago 507MB
nginx latest 5a9061639d0a 7 weeks ago 126MB
redis latest de25a81a5a0b 7 weeks ago 98.2MB
而且访问我们的私服也可以看到已经上传了镜像,http://192.168.59.133:5000/v2/_catalog
{"repositories":["design-parttern"]}