Try connecting to AzureCosmosDB Emulator for Docker with Java

Introduction

I tried using AzureCosmosDB Emulator, but I'm addicted to it, so I'll leave a note. AzureCosmosDB for Docker is only available from Docker for Windows.

Start AzureCosmosDB Emulator

I doubted my eyes, but I have 6GB ... If you are doing it on a shared NW, do it at night.

docker pull microsoft/azure-cosmosdb-emulator

Create a directory

md %LOCALAPPDATA%\CosmosDBEmulator\bind-mount

to start

docker run --name azure-cosmosdb-emulator --memory 2GB --mount "type=bind,source=%LOCALAPPDATA%\CosmosDBEmulator\bind-mount,destination=C:\CosmosDB.Emulator\bind-mount" --interactive --tty -p 8081:8081 -p 8900:8900 -p 8901:8901 -p 8902:8902 -p 10250:10250 -p 10251:10251 -p 10252:10252 -p 10253:10253 -p 10254:10254 -p 10255:10255 -p 10256:10256 -p 10350:10350 microsoft/azure-cosmosdb-emulator

An error occurs here

docker: Error response from daemon: failed to create endpoint azure-cosmosdb-emulator on network nat: HNS failed with error :The process cannot access the file. Another process is in use..

At first I didn't understand at all, but it seemed that it was covered with the port on the host side (McAfee in my case), and it worked well by changing the port setting when docker run.

If it starts up successfully, it will be in powershell mode and this will be output. ** Please note that when you cancel powershell mode, docker will be stopped. ** **

Key   : GremlinEndpoint
Value : {http://bd876a98b564:8901/, http://172.30.166.171:8901/}
Name  : GremlinEndpoint


Key   : TableEndpoint
Value : {https://bd876a98b564:8902/, https://172.30.166.171:8902/}
Name  : TableEndpoint


Key   : Key
Value : C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==
Name  : Key


Key   : Version
Value : 2.4.5.0
Name  : Version


Key   : IPAddress
Value : 172.30.166.171
Name  : IPAddress


Key   : Emulator
Value : CosmosDB.Emulator
Name  : Emulator


Key   : CassandraEndpoint
Value : {tcp://bd876a98b564:10350/, tcp://172.30.166.171:10350/}
Name  : CassandraEndpoint


Key   : MongoDBEndpoint
Value : {mongodb://bd876a98b564:10255/, mongodb://172.30.166.171:10255/}
Name  : MongoDBEndpoint


Key   : Endpoint
Value : {https://bd876a98b564:8081/, https://172.30.166.171:8081/}
Name  : Endpoint

Import certificate

Import the certificate to the host. If you do not follow this procedure, you will not be able to connect to the management screen with your browser.

C:\Users\xxxx\AppData\Local\CosmosDBEmulator\bind-mount>powershell .\importcert.ps1

Importing self-signed certificate generated by the Azure Cosmos DB Emulator runnning in container a9ed9a9e172b to the certificate store on XXXXXX

Try connecting to Explorer

You can open a web console that looks exactly like the Data Explorer in Azure Cosmos DB. Try to access the address + _explorer / index.html that came out as Endpoint in the previous log.

https://172.30.166.177:8081/_explorer/index.html

It is OK if such a screen appears.

image.png

Connect to Cosmos DB emulator from Java

When I try to connect to Cosmos DB in Java, I get the following error:

[pool-1-thread-1] ERROR com.microsoft.azure.documentdb.DocumentClient$DocumentDBThreadPoolExecutor  - Runnable execution exception
java.lang.IllegalStateException: com.microsoft.azure.documentdb.DocumentClientException: javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
	at com.microsoft.azure.documentdb.internal.routing.ClientCollectionCache.readCollection(ClientCollectionCache.java:35)
	at com.microsoft.azure.documentdb.internal.routing.ClientCollectionCache.getByName(ClientCollectionCache.java:41)
	at com.microsoft.azure.documentdb.internal.routing.CollectionCache$4.call(CollectionCache.java:148)
	at com.microsoft.azure.documentdb.internal.routing.CollectionCache$4.call(CollectionCache.java:145)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: com.microsoft.azure.documentdb.DocumentClientException: javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
	at com.microsoft.azure.documentdb.internal.GatewayProxy.performGetRequest(GatewayProxy.java:252)
	at com.microsoft.azure.documentdb.internal.GatewayProxy.doRead(GatewayProxy.java:93)
	at com.microsoft.azure.documentdb.internal.GatewayProxy.processMessage(GatewayProxy.java:371)
	at com.microsoft.azure.documentdb.DocumentClient$8.apply(DocumentClient.java:3102)
	at com.microsoft.azure.documentdb.internal.RetryUtility.executeDocumentClientRequest(RetryUtility.java:72)
	at com.microsoft.azure.documentdb.DocumentClient.doRead(DocumentClient.java:3108)
	at com.microsoft.azure.documentdb.DocumentClient.readCollection(DocumentClient.java:746)
	at com.microsoft.azure.documentdb.internal.routing.ClientCollectionCache.readCollection(ClientCollectionCache.java:33)
	... 7 more

The certificate was imported to the host by the procedure of "Import certificate", but it seems to be an error because the certificate is not in the Java KeyStore.

Export the certificate

There is no problem with the procedure at the following URL.

https://docs.microsoft.com/ja-jp/azure/cosmos-db/local-emulator-export-ssl-certificates

Import the certificate

Change to the% JAVA_HOME% / lib / security directory.

Add the certificate to the keystore.

keytool -keystore cacerts -importcert -alias documentdbemulatorcert -file documentdbemulatorcert.cer
Please enter the keystore password:changeit
owner: CN=localhost
Issuer: CN=localhost
Serial number: 2fe969231bd7868a419cdc4067fb931a
Validity start date: Tue Jul 23 09:27:31 GMT+09:00 2019 end date: Sun Jul 21 09:27:31 GMT+09:00 2024
Certificate finger print:
         SHA1: 3F:32:E0:2E:31:32:2E:C3:1B:29:11:82:F1:7B:01:21:31:A6:5D:99
         SHA256: 12:2D:B9:6A:38:44:DB:04:00:B9:AB:8E:35:A8:89:76:AD:18:0C:45:AE:01:42:3F:E6:5E:F8:21:80:53:AF:47
Signature algorithm name: SHA1withRSA
Subject public key algorithm:1024 bit RSA key
version: 3

Expansion:

#1: ObjectId: 2.5.29.17 Criticality=true
SubjectAlternativeName [
  DNSName: a9ed9a9e172b
  DNSName: localhost
  IPAddress: 172.30.166.177
  IPAddress: 127.0.0.1
  DNSName: 172.30.166.177
  DNSName: 127.0.0.1
]

Do you trust this certificate?[No]:  y
Certificate added to keystore

Make sure that the certificate was imported successfully

keytool -list -keystore cacerts

[Reference] Delete the certificate

keytool -delete -keystore cacerts -alias documentdbemulatorcert

in conclusion

Bringing 2G resources locally and adding to the certificate export / Keystore is rather troublesome, so I was worried about using it as it is ...

Recommended Posts

Try connecting to AzureCosmosDB Emulator for Docker with Java
Try AWS Lambda Runtime Interface Emulator with Docker Desktop for M1
[Java] How to test for null with JUnit
Try to link Ruby and Java with Dapr
Try to implement TCP / IP + NIO with JAVA
Docker Container Operations with Docker-Client API for Java
How to use nginx-ingress-controller with Docker for Mac
Try WildFly with Docker
Try connecting to the Autonomous Database with JDK6 (Java) + JDBC OCI Driver (type 2).
Settings for connecting to MySQL with Spring Boot + Spring JDBC
How to build docker environment with Gradle for intelliJ
Initialization of for Try to make Java problem TypeScript 5-4
Feel free to try Elasticsearch cluster with WSL2 + Docker
How to make Laravel faster with Docker for Mac
Try to build a Java development environment using Docker
Command to try using Docker for the time being
Java to play with Function
Try DB connection with Java
Try gRPC with Java, Maven
Connect to DB with Java
Connect to MySQL 8 with Java
Wait for the container service to start with docker healthcheck
Try Spark Submit to EMR using AWS SDK for Java
Investigated how to call services with Watson SDK for Java
Try Oracle Enterprise Manager Cloud Control with Docker Desktop for Windows
Install java with Ubuntu 16.04 based Docker
Java to learn with ramen [Part 1]
Environment construction with Docker for beginners
[Java] Points to note with Arrays.asList ()
Easily Docker Java applications with Jib
Try running cloudera manager with docker
Dare to challenge Kaggle with Java (1)
Try to speed up Java console program startup with GraalVM native-image
Try using Redis with Java (jar)
Enable OpenCV with java8. (For myself)
Run logstash with Docker and try uploading data to Elastic Cloud
I tried to interact with Java
Connecting to a database with Java (Part 1) Maybe the basic method
(For myself) Try creating a C # environment with docker + code-server, cloud9
Try to imitate marshmallows with MiniMagick
[For beginners] Minimum sample to update RecyclerView with DiffUtils in Java
[Java] Try to implement using generics
Try to extract java public method
Try bidirectional communication with gRPC Java
Try to implement Yubaba in Java
Java, arrays to start with beginners
Update MySQL from 5.7 to 8.0 with Docker
How to start Camunda with Docker
[For those who create portfolios] How to use binding.pry with Docker
For the time being, run the war file delivered in Java with Docker
Wait for PostgreSQL to start with Docker and then start the WEB service
[Java] How to turn a two-dimensional array with an extended for statement
Practice of Java programming basics-I want to display triangles with for statements ①
Creating a java web application development environment with docker for mac part1
Practice of Java programming basics-I want to display triangles with for statements ②
Create a java web application development environment with docker for mac part2
Interface Try to make Java problem TypeScript 7-3
How to share files with Docker Toolbox
Try to solve Project Euler in Java
How to compile Java with VsCode & Ant
Getting Started with Ruby for Java Engineers