java - Firestore has trouble initialize db in Docker - Stack Overflow

I'm making a Java application which will connect to a Firestore db on GCP to do some data read and

I'm making a Java application which will connect to a Firestore db on GCP to do some data read and write, it all worked well if I build and run with mvn, but when I build it into a Docker container, instantiating the Firestore instance will throw error

java.lang.IllegalArgumentException: Address types of NameResolver 'unix' for 'firestore.googleapis:443' not supported by transport

This is how my code looks like

String serviceAccountPath = "path to a json";

Credentials credentials;
try (FileInputStream serviceAccountStream = new FileInputStream(serviceAccountPath)) {
  credentials = GoogleCredentials.fromStream(serviceAccountStream);
}

firestore = FirestoreOptions.newBuilder()
  .setProjectId("project id")
  .setDatabaseId("my db name")
  .setCredentials(credentials)
  .setTransportOptions(
    FirestoreOptions.getDefaultTransportOptionsBuilder().build()
  )
  .build()
  .getService();

This is my Dockerfile

FROM maven:3.8.4-jdk-11

# Set the working directory to /
WORKDIR /

# Copy the Java source code into the container
COPY . /

ENV GRPC_DNS_RESOLVER="dns"
ENV GRPC_DEFAULT_SSL_PROVIDER="netty"
ENV GRPC_DEFAULT_TRANSPORT="netty"

RUN mvn compile 
RUN mvn package compile

# Expose the gRPC port
EXPOSE 50051
EXPOSE 443

# Run the gRPC service when the container starts
CMD ["java","-Dio.grpcty.shaded.ioty.transport.noNative=false", "-jar", "target/Backend-1.0-SNAPSHOT-jar-with-dependencies.jar"]

Again, this all worked if I run it on my Mac with mvn, but it is just not working with Docker.

I'm making a Java application which will connect to a Firestore db on GCP to do some data read and write, it all worked well if I build and run with mvn, but when I build it into a Docker container, instantiating the Firestore instance will throw error

java.lang.IllegalArgumentException: Address types of NameResolver 'unix' for 'firestore.googleapis:443' not supported by transport

This is how my code looks like

String serviceAccountPath = "path to a json";

Credentials credentials;
try (FileInputStream serviceAccountStream = new FileInputStream(serviceAccountPath)) {
  credentials = GoogleCredentials.fromStream(serviceAccountStream);
}

firestore = FirestoreOptions.newBuilder()
  .setProjectId("project id")
  .setDatabaseId("my db name")
  .setCredentials(credentials)
  .setTransportOptions(
    FirestoreOptions.getDefaultTransportOptionsBuilder().build()
  )
  .build()
  .getService();

This is my Dockerfile

FROM maven:3.8.4-jdk-11

# Set the working directory to /
WORKDIR /

# Copy the Java source code into the container
COPY . /

ENV GRPC_DNS_RESOLVER="dns"
ENV GRPC_DEFAULT_SSL_PROVIDER="netty"
ENV GRPC_DEFAULT_TRANSPORT="netty"

RUN mvn compile 
RUN mvn package compile

# Expose the gRPC port
EXPOSE 50051
EXPOSE 443

# Run the gRPC service when the container starts
CMD ["java","-Dio.grpcty.shaded.ioty.transport.noNative=false", "-jar", "target/Backend-1.0-SNAPSHOT-jar-with-dependencies.jar"]

Again, this all worked if I run it on my Mac with mvn, but it is just not working with Docker.

Share Improve this question edited Mar 7 at 20:53 Doug Stevenson 319k36 gold badges456 silver badges473 bronze badges Recognized by Google Cloud Collective asked Mar 7 at 20:50 YT.LuYT.Lu 317 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

It turns out I didn't use the correct CMD to start the server, replaced with

CMD ["mvn", "exec:java", "-Dexec.mainClass={mymainclass}"]

then it just worked the same way as localhost.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744910052a4600492.html

相关推荐

  • java - Firestore has trouble initialize db in Docker - Stack Overflow

    I'm making a Java application which will connect to a Firestore db on GCP to do some data read and

    1天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信