Spring data mongo DB: Premature end of stream error when connecting to docker mongodb - spring

I am running latest mongodb image using docker on my local machine. I start it like so
docker run -d -p 27017:49153 --name localhost-mongo \
-e MONGO_INITDB_ROOT_USERNAME=admin \
-e MONGO_INITDB_ROOT_PASSWORD=password mongo
I am also able to log into mongo shell like this
docker exec -it localhost-mongo mongo \
--host localhost \
-u admin \
-p password \
--authenticationDatabase admin some-db
But when I try to start my spring boot application which connects with Mongodb using spring data mongodb library I get below error immediately
[2022-07-14 10:32:03,131] [main] [INFO] [com.mon.dia.log.SLF4JLogger::info] - Cluster created with settings {hosts=[127.0.0.1:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms'}
Disconnected from the target VM, address: '127.0.0.1:56890', transport: 'socket'
[2022-07-14 10:33:31,124] [cluster-ClusterId{value='62cfa34bc781d73d03f70e58', description='null'}-127.0.0.1:27017] [INFO] [com.mon.dia.log.SLF4JLogger::info] - Exception in monitor thread while connecting to server 127.0.0.1:27017
com.mongodb.MongoSocketReadException: Prematurely reached end of stream
at com.mongodb.internal.connection.SocketStream.read(SocketStream.java:112) ~[mongodb-driver-core-4.0.5.jar:?]
at com.mongodb.internal.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:579) ~[mongodb-driver-core-4.0.5.jar:?]
at com.mongodb.internal.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:444) ~[mongodb-driver-core-4.0.5.jar:?]
at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:298) ~[mongodb-driver-core-4.0.5.jar:?]
at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:258) ~[mongodb-driver-core-4.0.5.jar:?]
at com.mongodb.internal.connection.CommandHelper.sendAndReceive(CommandHelper.java:83) ~[mongodb-driver-core-4.0.5.jar:?]
at com.mongodb.internal.connection.CommandHelper.executeCommand(CommandHelper.java:33) ~[mongodb-driver-core-4.0.5.jar:?]
at com.mongodb.internal.connection.InternalStreamConnectionInitializer.initializeConnectionDescription(InternalStreamConnectionInitializer.java:103) ~[mongodb-driver-core-4.0.5.jar:?]
at com.mongodb.internal.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:60) ~[mongodb-driver-core-4.0.5.jar:?]
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:128) ~[mongodb-driver-core-4.0.5.jar:?]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) [mongodb-driver-core-4.0.5.jar:?]
at java.lang.Thread.run(Thread.java:834) [?:?]
I tried so many ways to at least see what is happening by trying
-Dlogging.level.org.springframework.data=DEBUG
-Dlogging.level.com.mongodb=DEBUG
Debug logs of spring data don't show anything peculiar. No logs from com.mongodb appear on debug level.
Below are my spring configuration properties
spring:
data:
mongodb:
database: challengeAPI
host: 127.0.0.1
port: 27017
username: admin
password: password
authentication-database: admin
max-wait-time: 30000
Please help. Totally stuck just trying to connect to local mongodb.

Why are you port-mapping from 49153? The documentation states that:
The MongoDB server in the image listens on the standard MongoDB port, 27017.
Change it to:
-p 27017:27017

Related

windows redis-client connect to docker server failed

I am using windows10, redis-64bit, I started a redis container with command:
docker run --name myredis -d redis redis-server --appendonly yes
when I try to connect to this container using:
redis-cli -h 192.168.99.1 -p 6379
it shows:
Could not connect to Redis at 192.168.99.1:6379: Unknown error
here, 192.168.99.1 is my virtual machine ip address, anyone know how to solve this issue, thanks!
To connect to a redis container from a remote server you should do the following:
Start redis container on host (192.168.99.1):
docker run --name myredis -p 7000:6379 -d redis redis-server
Connect via remote server:
redis-cli -h 192.168.99.1 -p 7000

Gitlab CI cannot connect spring application to postgres

I have following stage in gitlab yml file.
integration-tests:
image: mydocker-hub-id/mvn-intergration-tests-image:latest
stage: test
services:
- postgres
script:
- export PGPASSWORD=$POSTGRES_PASSWORD
- psql -h "postgres" -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "SELECT 'OK' AS status;"
- cd source_code
- pg_isready
- mvn verify -P test-ci -DskipUTs=true
When job is executed for command pg_isready I have response 5432 no response.
When tests are executed from each intergration test I have error:
org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
Image used in this stage is built based on postgres image which has additionally installed maven and java 11.
What could be the issue that spring application cannot connect with postgres server. Command postgres --version displays proper version. Also command
"psql -h "postgres" -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "SELECT 'OK' AS status;"
returns the OK result.
Spring db connection in app.properties is following:
spring.datasource.url=jdbc:postgresql://localhost:5432/test
I found in gitlab documentation details about postgres service configuration
https://docs.gitlab.com/ee/ci/services/postgres.html?fbclid=IwAR1eDlOQ4ACn6zqw1auIetB07JRcCGi1Pjl-hpfGBM45ujvkImm6fHSSbgg
The db url should contain the name of host postgres instead of localhost.
I found gitlab-runner is running the postgresql docker image as postgres and this is working changing jdbc:postgresql://localhost:5432/dbname into jdbc:postgresql://postgres:5432/dbname.

RabbitMQ on docker: Failed to check/redeclare auto-delete queue(s)

In my java spring-boot app time to time I get this error.
Restarting docker and starting container again fixes it.
But I want to know how can I deal with it faster?
Docker logs do not give me anything - for example issue occurred now (14:25) but logs are old:
2019-02-04 13:19:41.885 [error] <0.1509.0> closing AMQP connection <0.1509.0> (172.17.0.1:55060 -> 172.17.0.2:5672 - rabbitConnectionFactory#6049c421:648):
missed heartbeats from client, timeout: 60s
2019-02-04 13:19:41.903 [error] <0.1517.0> closing AMQP connection <0.1517.0> (172.17.0.1:55064 -> 172.17.0.2:5672 - rabbitConnectionFactory#575a1719:1056):
missed heartbeats from client, timeout: 60s
PS C:\Users\user> docker logs -f rabbit-fox
I created this container by following command:
docker run -d --hostname my-rabbit --name rabbit-fox -p 5672:5672 -p 8090:15672 rabbitmq:3-management
I used a different port to expose 4369:4369, you can try. I dont know the reason but its worked for me. In the rabbit documentation is said about this port, you can see more in https://www.rabbitmq.com/networking.html

unable to start kafka and zookeeper using docker; ports 9092 and 2181 are already allocated

I am trying to statrt kafka and zookeeper using this docker file
version: '2'
services:
kafka:
image: landoop/fast-data-dev:cp3.3.0
hostname: kafka-host
ports:
- 29092:29092
- 3030:3030 # Landoop UI
- 8081-8083:8081-8083 # REST Proxy, Schema Registry, Kafka Connect ports
- 9581-9585:9581-9585 # JMX Ports
- 2181:2181 # Zookeeper
- 9092:9092 # Kafka Broker
environment:
ADV_HOST: 127.0.0.1
RUNTESTS: 0
FORWARDLOGS: 0
SAMPLEDATA: 0
but I keep on receiving this error message
Cannot start service kafka: b'driver failed programming external connectivity on endpoint kafka-docker_kafka_1 (a741da2d21a00bb752fc169d579fa39bcdeef0cc88ec560d2e93b8fd287b8b5a): Error starting userland proxy: Bind for 0.0.0.0:9092 failed: port is already allocated'
even after I remove all the images and containers ...
with "sudo lsof -i tcp:9092"
I see some process are going on ... but even after I kill them, using kill -9 PIDsome other appear ...
I solved the problem; kafka and zookeeper were actually already installed locally and brew were running them in the background ... when I uninstalled them, everything was fine!
It seems that one of your containers is still using port 9092 (as sudo lsof -i tcp:9092 shows). Run docker ps to be sure that there is no container running using this port.
Moreover, stopping containers using kill -9 command is not the right way :
if you're using docker-compose (as your example shows), you can run docker-compose down to stop all services (therefore all containers defined in your file). Alternatively, if you want to shutdown a container using only docker command, you have to run docker stop <container_id_or_name>, but I do not recommend that if you're using docker-compose
when you issue a kill -9 command, you're experiencing that containers are restarting over and over because you probably have defined a restart policy on your service (https://docs.docker.com/compose/compose-file/compose-file-v2/#restart). The container started after the other one have exited (caused by kill -9) is using the same ports as the previous, so port 9092 is always used

Cannot connect to Postgres from a SpringBoot app running in a docker container

I have a Spring Boot application that is running in a docker container. From this application I am trying to connect to a Postgres DB that is running in localhost.
I am getting Connection refused error.
I tried solution #1 & #3 mentioned in the below post, but that is also not working.
Using Docker to launch web app, can't connect to Postgresql DB?
What I tried so far
Spring Boot application.properties has Postgres url as localhost:5432 and
docker run --add-host [dockerhost ip] -it and docker run --net=host -it
Spring Boot application.properties has Postgres url as [dockerhost ip]:5432
and docker run --add-host [dockerhost ip] -it and docker run --net=host -it
Neither of the above is working.
I am using Docker for Mac 1.12.5

Resources