我在 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>
然后在 Docker 工具箱中运行: 1.mvn干净安装 2.mvn清理包-Dbuild=docker
它在 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/