java - Docker 镜像无法将我的 JSON 凭证 key 添加到我的 Google Cloud Natural Language 服务客户端

标签 java docker google-cloud-platform

我在 Eclipse 中创建了一个 Spring Boot Java 应用程序,该应用程序连接到 Google Cloud Natural Language 服务来为我分析文本。 我创建了它的 Docker 镜像,但它无法运行,但它在 Eclipse 中对我来说可以工作。

我像这样读取 JSON key :

InputStream inputStream = GoogleNlpApplication.class.getClassLoader().getSystemResourceAsStream("AvayaNlp-e422dbca92e5.json");

credentialsProvider = FixedCredentialsProvider.create(ServiceAccountCredentials.fromStream(inputStream));

我的 Spotify Docker 插件的 pom 文件:

<profiles>
        <profile>
            <id>docker-build</id>
            <activation>
                <property>
                    <name>build</name>
                    <value>docker</value>
                </property>
            </activation>

            <build>
                <plugins>
                    <plugin>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                    </plugin>

                    <plugin>
                        <groupId>com.spotify</groupId>
                        <artifactId>docker-maven-plugin</artifactId>
                        <version>1.2.0</version>
                        <configuration>
                            <imageName>googlenlp2vi</imageName>
                            <baseImage>java</baseImage>
                            <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
                            <!-- copy the service's jar file from target into the root directory 
                                of the image -->
                            <resources>
                                <resource>
                                    <targetPath>/</targetPath>
                                    <directory>${project.build.directory}</directory>
                                    <include>${project.build.finalName}.jar</include>
                                </resource>
                            </resources>
                        </configuration>

                        <executions>

                            <execution>
                                <id>default</id>
                                <!-- phase>install</phase -->
                                <phase>package</phase>
                                <goals>
                                    <goal>build</goal>
                                    <!-- goal>push</goal -->
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
                <finalName>googlenlp2vi</finalName>
            </build>
        </profile>
    </profiles>

应用程序的目录: Directory

然后在 Docker 工具箱中运行: 1.mvn干净安装 2.mvn清理包-Dbuild=docker

我得到的错误: enter image description here

它在 Eclipse 上运行良好,docker 镜像是否没有使用 java 文件的导入?

更新解决方案:

我结束了在资源中创建一个新目录:

src
 |
  main
   |
    resources
     |
      test
       |
        AvayaNlp-e422dbca92e5.json

然后使用:

ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource[] resources = resolver.getResources("classpath*:test/*.json");

        InputStream inputStream = null;

        for (Resource r : resources) {
            inputStream = r.getInputStream();
        }

credentialsProvider = FixedCredentialsProvider.create(ServiceAccountCredentials.fromStream(inputStream));

现在,docker 镜像可以访问 jar 中的 json key ,但如果资源中的文件夹中有多个 JSON,它将获取所有 JSON。

最佳答案

您需要在运行此镜像时 JAR 期望找到的位置提供 AvayaNlp-e422dbca92e5.json

我对 docker-maven-plugin 不太熟悉,无法为您提供精确的指导,但假设 AvayaNlp-e422dbca92e5.json 位于 ${ PWD}/secrets 并且您希望将其安装到容器镜像中的 /secrets/docker run 命令将采用以下形式:

docker run ... --volume=${PWD}/secrets:/secrets ... [[your-image]]

您如何运行容器来生成错误?

Maven 会在创建容器镜像后运行它吗?

呵呵!

关于java - Docker 镜像无法将我的 JSON 凭证 key 添加到我的 Google Cloud Natural Language 服务客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55554476/

相关文章:

docker - 通过公司局域网中的DNS名称访问Docker容器

authentication - 如何获取经过身份验证的事件 gcloud 帐户?

java - 从 Firebase 加载大型 json 时,Android 应用程序返回 java.lang.OutOfMemoryError

java - 运行 JUnit 测试时使用 PowerMockito 获取内部状态错误

docker - 单个 docker 容器的 cpu 性能略优于其主机 : Why?

谷歌容器生成器 : How to install govendor dependencies during build step?

php - 在Kubernetes中部署Wordpress的存储策略

javascript - 下拉列表的滚动条

java - 方法解释

amazon-web-services - EC2 的 AWS 免费套餐限制