簡體   English   中英

fabric8 springboot 完整示例

[英]fabric8 springboot full example

我想弄清楚如何使用fabric8 docker-maven-plugin 構建一個spring boot docker鏡像。 該文檔包含位和字節,我顯然遺漏了一些東西。 有人有完整的 pom.xml 示例嗎?

如果您只是想快速入門,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命令。

這是使用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>

您可以按照此鏈接獲取分步說明

但是,您可以使用我發現使用起來更方便的 Dockerfile,而不是從 fabric8 maven 插件構建 Image,這就是您會注意到的原因

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

文件

    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 中心注冊表,則可以使用此鏈接

如果您不必使用該插件,我建議您使用 spotify 的docker-maven-plugin 設置后,您可以執行mvn clean package docker:build來構建 docker 映像。

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

是內聯匯編配置的示例

將此行添加到您的 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>
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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM