由于我的Spring Boot工程使用的是maven-assembly-plugin插件进行打包的,所以这里结合assembly和docker进行配置。
assembly的好处是可以将配置和Jar包分离,也可将依赖包分离,缩小项目jar的大小。
maven-assembly-plugin插件
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>${assembly.version}</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
<archive>
<manifest>
<mainClass>cn.com.ctcloud.microservice.Application</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
assembly.xml文件内容
<?xml version='1.0' encoding='UTF-8'?>
<assembly>
<id>assembly</id>
<formats>
<format>dir</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>src/main/assembly/bin</directory>
<outputDirectory>bin</outputDirectory>
<fileMode>0755</fileMode>
</fileSet>
<fileSet>
<directory>src/main/assembly/config</directory>
<outputDirectory>config</outputDirectory>
<fileMode>0755</fileMode>
</fileSet>
<fileSet>
<directory>target</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
<fileSet>
<directory>src/lib</directory>
<outputDirectory>lib</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
<fileSet>
<directory>target/lib</directory>
<outputDirectory>lib</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
<!-- 复制Dockerfile到发布目录 -->
<fileSet>
<directory>src/main/docker</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>Dockerfile</include>
</includes>
</fileSet>
</fileSets>
</assembly>
docker-maven-plugin插件
构建镜像可以使用一下两种方式,第一种是将构建信息指定到 POM 中,第二种是使用已存在的 Dockerfile 构建。
- 第一种方式,支持将 FROM, ENTRYPOINT, CMD, MAINTAINER 以及 ADD 信息配置在 POM 中,不需要使用 Dockerfile 配置
- 第二种方式,如果使用 VOLUME 或其他 Dockerfile 中的命令的时候,需要创建一个 Dockerfile,并在 POM 中配置 dockerDirectory 来指定路径即可。
注意:开发机使用docker插件时,要开启服务端Docker的远程API,否则需要开发机安装Docker。
<!-- 使用Maven插件直接将应用打包为一个Docker镜像 -->
<!-- mvn clean install docker:build -DskipTests -->
<!-- mvn clean install docker:push -DskipTests -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<!--将插件绑定在某个phase执行,用户只需执行mvn package,自动执行mvn docker:build <executions>
<execution> <id>buid-image</id> <phase>package</phase> <goals> <goal>build</goal>
</goals> </execution> </executions> -->
<configuration>
<!--maven配置文件中的server -->
<serverId>docker-harbor</serverId>
<!-- docker harbor地址 -->
<registryUrl>http://${docker.repostory}</registryUrl>
<!--必须配置dockerHost标签(除非配置系统环境变量DOCKER_HOST) -->
<dockerHost>http://${docker.api}</dockerHost>
<!--镜像名称,若推送给harbor,必须指定docker.repostory -->
<imageName>${docker.repostory}/${docker.registry.name}/${project.name}:${project.version}</imageName>
<!-- Dockerfile文件位置 -->
<dockerDirectory>${project.basedir}/target/${project.build.finalName}-assembly/${project.build.finalName}</dockerDirectory>
</configuration>
</plugin>
其中,插件的属性配置如下:
<properties>
<!--docker插件-->
<!-- docker harbor地址:端口号 -->
<docker.repostory>192.168.1.111:7080</docker.repostory>
<docker.api>192.168.1.111:2375</docker.api>
<!-- 设置docker镜像的前缀(要在Harbor中创建对应的项目) -->
<docker.registry.name>ctcloud</docker.registry.name>
</properties>
撰写Dockerfile文件
# 自己构建的基于centos6.7 + jdk1.8的镜像
FROM ctcloud/centos:6.7
# 复制整个assembly发布目录到容器
copy / /root/ctcloud-tenant
# 暴露镜像的端口供主机做映射
EXPOSE 8210
# 需要执行的命令
RUN chmod +x /root/ctcloud-tenant/bin/*.sh
ENTRYPOINT /root/ctcloud-tenant/bin/start.sh && tail -F /root/ctcloud-tenant/nohup.out
####################################################
# 启动容器(maven构建后,要等一段时间再启动容器,否则启动会失败)
# docker run -id --name tenant -p 8210:8210 192.168.1.111:7080/ctcloud/ctcloud-tenant:1.0.0
# docker run --log-driver=fluentd --log-opt fluentd-address=192.168.1.111:24224 --log-opt tag=httpd.access -id --name tenant -p 8210:8210 192.168.1.111:7080/ctcloud/ctcloud-tenant:1.0.0
# 查看容器状态
# docker ps -a
# 进入容器
# docker exec -it tenant /bin/bash
制作Docker镜像
现在执行mvn clean install -X docker:build -DskipTests打包命令,即可制作镜像。
推送到Harbor仓库
由于我要推送的仓库是私有的,需要用户名密码,所以,要在maven的配置文件(setting文件)中,添加如下配置:
<!-- docker harbor 配置 -->
<server>
<id>docker-harbor</id>
<username>admin</username>
<password>admin</password>
<configuration>
<email>charles0902@163.com</email>
</configuration>
</server>
配置好之后,执行命令:mvn clean install docker:push -DskipTests
在Harbor仓库查看推送镜像内容

Spring Boot工程结合Docker制作并推送镜像
1183

被折叠的 条评论
为什么被折叠?



