fabric8 springboot完整的例子

我想弄清楚如何使用fabric8 docker-maven-plugin来构build一个Spring Boot Docker镜像。 该文档包含位和字节,我显然缺less的东西。 有没有人有一个完整的pom.xml的例子呢?

如果你不需要使用这个插件,我build议使用spotify的docker-maven-plugin 。 安装后,你可以做mvn clean package docker:build来构builddocker镜像。

你的pom.xml如下所示:

 ... <properties> <docker.image.prefix>springio</docker.image.prefix> </properties> <build> <plugins> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.11</version> <configuration> <imageName>${docker.image.prefix}/${project.artifactId}</imageName> <dockerDirectory>src/main/docker</dockerDirectory> <buildArgs> <finalName>${project.build.finalName}.jar</finalName> </buildArgs> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> </plugins> </build> ... 

src/main/docker Dockerfile中的Dockerfile看起来像这样:

 FROM openjdk:8u102-jre ARG finalName ADD $finalName /my-app.jar ENTRYPOINT ["java","-jar","/my-app.jar"] 

参考文献:

https://spring.io/guides/gs/spring-boot-docker

https://github.com/spotify/docker-maven-plugin

如果您只想快速入门,那么fabric8 docker-maven-plugin文档是非常难以挖掘的,所以这里有一个快速的例子来说明如何构buildDocker镜像。

首先,确保docker在你的path上,Docker守护进程正在运行。 运行docker ps并确保如下的响应:

 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 

把它添加到你的pom.xml中,并用mvn clean package docker:build

 <build> <plugins> <plugin> <groupId>io.fabric8</groupId> <artifactId>docker-maven-plugin</artifactId> <configuration> <verbose>true</verbose> <images> <image> <!-- Replace your-project-name --> <name>your-project-name:${project.version}</name> <build> <!-- This is the same FROM used in a Dockerfile --> <from>vixns/java8</from> <!-- This is the same ENTRYPOINT used in a Dockerfile --> <entryPoint> <exec> <arg>java</arg> <!-- This extra argument is so Tomcat can start faster due to lack of entropy --> <arg>-Djava.security.egd=file:/dev/./urandom</arg> <arg>-jar</arg> <!-- /maven/ is the default dir that the plugin copies your artifact to --> <arg>/maven/${project.artifactId}.${project.packaging}</arg> </exec> </entryPoint> <assembly> <!-- This is a predefined assembly.xml that will only copy your final artifact to the Docker image --> <descriptorRef>artifact</descriptorRef> </assembly> </build> </image> </images> </configuration> </plugin> </plugins> </build> 

注意:如果你想使用mvn docker:start命令,你需要额外的设置。 你不必使用它,你可以使用标准的docker命令。

这是使用fabric8 的工作示例之一。 这个项目使用了spring boot docker镜像,然后把它连接到mongodb。

  <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>io.fabric8</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.20.0</version> <configuration> <!--<dockerHost>tcp://REMOTE_IP:2375</dockerHost> --> <images> <image> <name>springboot-mongo-dockerimage:${project.version}</name> <alias>springboot-mongo-dockerimage</alias> <build> <dockerFileDir>${project.basedir}</dockerFileDir> </build> <run> <namingStrategy>alias</namingStrategy> <dependsOn> <container>mongo</container> </dependsOn> <links> <link>mongo</link> </links> <ports> <port>8080:8080</port> </ports> </run> </image> </images> </configuration> <executions> <execution> <id>start</id> <phase>pre-integration-test</phase> <goals> <goal>stop</goal> <goal>build</goal> <goal>start</goal> </goals> </execution> </executions> </plugin> </plugins> </build> 

您可以按照此链接一步一步的指示

但是,而不是从fabric8 maven插件构build图像,您可以使用Dockerfile,我发现更方便使用,这就是为什么你会注意到

 <build> <dockerFileDir>${project.basedir}</dockerFileDir> </build> 

DokcerFile

  FROM java:8 VOLUME /tmp ADD target/Boot-0.0.1-SNAPSHOT.jar app.jar EXPOSE 8080 RUN bash -c 'touch /app.jar' ENTRYPOINT ["java","-Dspring.data.mongodb.uri=mongodb://mongo/test", "-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"] 

如果你想把你的图片推送到Docker中心registry,那么你可以使用这个链接 。