[英]Connection refused trying to connect to mongoDB docker instance
I'm trying to deploy a simple app with a MongoDB with JPA, but I can't make it work right.我正在尝试使用 MongoDB 和 JPA 部署一个简单的应用程序,但我无法使其正常工作。
When I deploy the app in a docker container, I keep getting the following error:当我在 docker 容器中部署应用程序时,我不断收到以下错误:
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:这是我用来构建映像的 dockerfile:
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:还有我用来运行mongoDB的docker-compose.yml:
# 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):还有我的 JPA 存储库(如上所述,是 jpa class 和实体的名称):
public interface <Name>JPA extends MongoRepository<<Name>Entity, Integer>{
}
And finally, I have defined the following properties under application.properties:最后,我在 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.我能够连接到 localhost:8081 以查看 mongo-express 界面,但除此之外,我什么也没看到。
UPD : use mongo:27017
for host property instead of two separate values. UPD :使用mongo:27017
作为主机属性而不是两个单独的值。
Original answer:原答案:
localhost
for a container is the container.容器的localhost
是容器。 Since you are having mongodb and the app in different containers, they can't connect this way.由于您在不同的容器中拥有 mongodb 和应用程序,因此它们无法以这种方式连接。 You can because you connect from the host via mapped ports.您可以,因为您通过映射端口从主机连接。 Simply change localhost
to mongo
and you should be all right.只需将localhost
更改为mongo
可以了。
If you are trying to connect with containerized MongoDB docker instance with localhost then you might face this issue.如果您尝试使用 localhost 连接容器化 MongoDB docker 实例,那么您可能会遇到此问题。
Solution:解决方案:
In place of代替
mongodb://localhost:27017
To connect the host out of the box by using the special DNS name: host.docker.internal使用特殊的 DNS 名称连接开箱即用的主机: host.docker.internal
mongodb://host.docker.internal:27017
Replacing localhost with host.docker.internal will do the trick.用host.docker.internal替换localhost就可以了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.