Py学习  »  docker

Docker映像无法将我的JSON凭据密钥添加到我的Google云自然语言服务客户端

Qwisatz • 4 年前 • 585 次点击  

我为它创建了一个Docker映像,它无法运行,但它在Eclipse中对我有效。

我在JSON密钥中读到如下内容:

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

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

<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 clean package-Dbuild=docker

我得到的错误是: enter image description here

更新解决方案:

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

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键了,不过如果资源中的文件夹中有多个json键,它将获取所有json键。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/57162
 
585 次点击  
文章 [ 2 ]  |  最新文章 4 年前
Akash Sharma
Reply   •   1 楼
Akash Sharma    5 年前

在Dockerfile中,在适当的位置添加json文件。 ADD dockerfile命令。

DazWilkin
Reply   •   2 楼
DazWilkin    5 年前

你需要 AvayaNlp-e422dbca92e5.json 在运行此映像时JAR希望找到它的位置可用。

我对 docker-maven-plugin 为你提供精确的指导,但是,假设 AvayaNlp-e422dbca92e5.json格式 是在 ${PWD}/secrets /secrets/ 在容器图像中 docker run

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

如何运行容器以生成错误?

Maven是否在创建容器映像后运行它?

啊!