I'm trying to deploy a simple app with a MongoDB with JPA, but I can't make it work right.
When I deploy the app in a docker container, I keep getting the following error:
2020-10-05 18:47:39.770 INFO 1 --- [localhost:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server localhost:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70) ~[mongodb-driver-core-4.0.5.jar!/:na]
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:127) ~[mongodb-driver-core-4.0.5.jar!/:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) ~[mongodb-driver-core-4.0.5.jar!/:na]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_212]
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_212]
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_212]
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_212]
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_212]
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_212]
at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_212]
at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:63) ~[mongodb-driver-core-4.0.5.jar!/:na]
at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:79) ~[mongodb-driver-core-4.0.5.jar!/:na]
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65) ~[mongodb-driver-core-4.0.5.jar!/:na]
... 3 common frames omitted
This is the dockerfile I use to build the image:
FROM openjdk:8-jdk-alpine
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
And the docker-compose.yml I used to run mongoDB:
# Use root/example as user/password credentials
version: '3.1'
services:
mongo:
image: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: example
My entities all follow the following pattern (being the name of the entity):
@Document
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class <Name>Entity {
@Id
private Integer <Name>Id;
// More data
}
And my JPA Repositories (as above, being the name of the jpa class & entity):
public interface <Name>JPA extends MongoRepository<<Name>Entity, Integer>{
}
And finally, I have defined the following properties under application.properties:
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=api-database
spring.data.mongodb.username=root
spring.data.mongodb.password=example
spring.data.mongodb.repositories.enabled=true
I am able to connect to localhost:8081 to see the mongo-express interface, but appart from that, I don't see anything.
UPD : use mongo:27017
for host property instead of two separate values.
Original answer:
localhost
for a container is the container. Since you are having mongodb and the app in different containers, they can't connect this way. You can because you connect from the host via mapped ports. Simply change localhost
to mongo
and you should be all right.
If you are trying to connect with containerized MongoDB docker instance with localhost then you might face this issue.
Solution:
In place of
mongodb://localhost:27017
To connect the host out of the box by using the special DNS name: host.docker.internal
mongodb://host.docker.internal:27017
Replacing localhost with host.docker.internal will do the trick.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.