fabric8 springboot完整示例

时间:2016-08-24 14:33:41

标签: docker spring-boot spring-boot-docker-plugin

我试图弄清楚如何使用fabric8 docker-maven-plugin构建一个spring boot docker镜像。文档包含位和字节,我显然遗漏了一些东西。有人有一个完整的pom.xml示例吗?

5 个答案:

答案 0 :(得分:5)

如果您想快速入门,那么fabric8-maven-plugin文档很难理解,所以这里有一个快速示例,说明了构建Docker镜像所需的一切。

首先,确保路径上有docker并且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>fabric8-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命令。

答案 1 :(得分:2)

如果您不必使用该插件,我建议使用spotify的docker-maven-plugin。设置完成后,您可以mvn clean package docker:build来构建泊坞窗图像。

你的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>
...

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

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

答案 2 :(得分:0)

Here is one of使用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>

您可以follow this link分步说明

然而,不是从fabric8 maven插件构建Image,而是可以使用我觉得更方便使用的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中心注册表,则可以使用this link

答案 3 :(得分:0)

Here是内联汇编配置的示例

将此行添加到您的pom文件中,并使用mvn docker:build

<plugin>
    <groupId>io.fabric8</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>0.20.0</version>
    <configuration>
        <verbose>true</verbose>
        <images>
            <image>
                <name>demo/helloworld</name>
                <build>
                    <from>openjdk</from>
                    <tags>
                        <tag>latest</tag>
                        <tag>${project.version}</tag>
                    </tags>
                    <assembly>
                        <descriptorRef>artifact</descriptorRef>
                    </assembly>
                    <cmd>java -jar maven/${project.artifactId}-${project.version}.jar</cmd>
                </build>
            </image>
        </images>
    </configuration>
</plugin>

答案 4 :(得分:0)

Dockerfile: 
... 
ADD target/<file-name>.jar <dest-dir> 
...

Dockerfile的位置:src / main / docker
Maven io.fabric8 docker插件配置

<configuration>
    <images>
        <image>
            <name>${docker.image.prefix}/${docker.image.name}</name>
            <alias>${project.artifactId}</alias>
            <build>
                <contextDir>${project.basedir}</contextDir>
                <tags>
                    <tag>latest</tag>
                </tags>
            </build>
        </image>
    </images>
    <resources>
        <resource>
            <targetPath>${project.basedir}</targetPath>
            <directory>${project.build.directory}</directory>
            <include>${project.build.finalName}.war</include>
        </resource>
    </resources>
</configuration>