【问题标题】:Dockerizing spring boot application using JIB plugin使用 JIB 插件对 Spring Boot 应用程序进行 Dockerizing
【发布时间】:2019-06-08 13:32:43
【问题描述】:

我正在使用 maven jib 插件来对接我基于 Spring Boot 的应用程序。

https://github.com/GoogleContainerTools/jib/tree/master/jib-maven-plugin

<plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-resources-plugin</artifactId>
                        <executions>
                            <execution>
                                <phase>generate-sources</phase>
                                <goals>
                                    <goal>copy-resources</goal>
                                </goals>
                                <configuration>
                                    <resources>
                                        <resource>
                                            <directory>src/main/resources/static</directory>
                                        </resource>
                                    </resources>
                                    <outputDirectory>${project.build.directory}/webapp/static</outputDirectory>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>com.google.cloud.tools</groupId>
                        <artifactId>jib-maven-plugin</artifactId>
                        <version>0.9.13</version>
                        <executions>
                            <execution>
                                <phase>package</phase>
                                <goals>
                                    <goal>build</goal>
                                </goals>
                            </execution>
                        </executions>
                        <configuration>
                            <from>
                                <image>${base.image}</image>
                            </from>
                            <to>
                                <image>${registry}/${repository}/${image}:${version}</image>
                            </to>
                            <extraDirectory>${project.build.directory}/webapp</extraDirectory>
                        </configuration>
                    </plugin>

我在 JIB 插件中没有 args 或 Entrypoint。我想通过参数来控制它。

在运行“mvn clean install”时,我在日志中看到以下行。

Container entrypoint set to [java, -cp, /app/resources:/app/classes:/app/libs/*, com.test.Application]

我尝试将 --spring.config.location 作为程序参数传递如下。但它没有选择我的 application.properties。我尝试修改起始类名以检查它是否正常工作,但它仍在使用 com.test.Application。看起来,这里没有考虑-c。

docker 运行 -v /local/path/config/:/secrets/ 图片:1.0 bash "java -cp /app/libs/*:/app/resources/:/app/classes/ -Xmx2g -Xms2g com.test.Application --spring.config.location=/secrets/application.yml"

【问题讨论】:

  • 如何将/secrets/application.yml 注入容器中?

标签: spring-boot docker jib


【解决方案1】:

了解问题,当使用 jib 作为 maven 插件时

然后 entryPoint 将在容器内传递,但似乎 jib 插件没有选择它。

因此需要对位置的参数访问进行以下更改:

           <plugin>
            <groupId>com.google.cloud.tools</groupId>
            <artifactId>jib-maven-plugin</artifactId>
            <version>2.2.0</version>
            <configuration>
                <to>
                    <image>image-url</image>
                </to>

                <container>
                    <creationTime>${maven.build.timestamp}</creationTime>
                    <mainClass>com.package.SpringBootMainClass</mainClass>
                    <args>
                        <arg>--spring.config.location=/demo/location/application.yml</arg>
                    </args>
                </container>

            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>build</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

【讨论】:

    【解决方案2】:

    由于您使用 Jib 构建容器映像,因此默认入口点是

    ENTRYPOINT ["java", jib.container.jvmFlags, "-cp", "/app/resources:/app/classes:/app/libs/*", jib.container.mainClass]
    

    jib 也使用仅包含运行时依赖项的 distroless 映像。所以你不能在你的容器中运行bash

    这应该可以完成工作。

    docker run -e "spring.config.location=/secrets/application.yml" -v /local/path/config/:/secrets/ IMAGE:1.0
    

    【讨论】:

      猜你喜欢
      • 2018-12-24
      • 2016-07-13
      • 1970-01-01
      • 2018-07-03
      • 1970-01-01
      • 1970-01-01
      • 2021-07-31
      • 2020-09-02
      • 1970-01-01
      相关资源
      最近更新 更多