赞
踩
修改daemon.json文件,开放端口2375
"hosts": ["tcp://0.0.0.0:2375", "unix:///var/run/docker.sock"]
我的文件如下所示:
{
"insecure-registries":["10.xx.xx.xx:5000"],
"hosts": ["tcp://0.0.0.0:2375", "unix:///var/run/docker.sock"]
}
{
"graph":"/Data/docker"
}
在项目的pom文件引入docker-maven-plugin插件
<plugin> <!-- https://mvnrepository.com/artifact/com.spotify/docker-maven-plugin --> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>1.2.2</version> <executions> <!-- 当mvn执行install操作的时候,执行docker的build和push --> <execution> <id>build</id> <phase>install</phase> <goals> <goal>build</goal> <!-- <goal>push</goal>--> </goals> </execution> </executions> <configuration> <!-- 连接到 带docker环境的linux服务器 编译image --> <dockerHost>http://10.xx.xx.xx:2375</dockerHost> <!--格式:私有仓库/镜像名称:版本号, 如果要执行push操作, 那么镜像名称必须为私有仓库为前缀,不然无效。--> <imageName>10.xx.xx.xx:5000/${project.artifactId}:${docker.tag}</imageName> <!--指定dockerfile文件路径--> <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory> <!-- 指定jar包路径,这里对应Dockerfile中复制 jar 包到 docker 容器指定目录配置,也可以写到 Docokerfile 中 --> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> <!--跳过测试--> <!-- push到 docker hub 开始 --> <!-- serverId 这个是配置在maven的setting.xml中私服的登录账户信息--> <!-- <serverId>docker-hub</serverId>--> <retryPushCount>3</retryPushCount> <retryPushTimeout>2000</retryPushTimeout> <registryUrl>10.xx.xx.xx:5000</registryUrl> <!-- 是否自動推送到Registry Server, 建議手動推送 --> <pushImage>${docker.push}</pushImage> <!-- push到 docker hub 结束 --> </configuration> </plugin>
重要配置解释:
在本地项目的src/main下创建docker目录,在docker目录下创建dockerfile文件
FROM 10.xx.xx.xx:5000/java
VOLUME /tmp
ENV LANG C.UTF-8
COPY *.jar app.jar
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo "Asia/Shanghai" > /etc/timezone
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom", "-Djava.util.Arrays.useLegacyMergeSort=true", "-jar","app.jar"]
此时,对该项目进行mvn install
时,相当于会同时进行docker:build的动作,将本地项目jar包构建成镜像到服务器中
如果每个服务的镜像构建没有差异,可以将dockerfile从子项目中抽出来,就不需要在每一个项目模块中都创建相同的文件,直接放在项目根路径下就可以进行通用,同时修改pom配置
<!--指定dockerfile文件路径-->
<dockerDirectory>${project.parent.basedir}/docker</dockerDirectory>
此时的dockerfile存储位置为项目父级目录的docker文件夹下。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。