英文:
Connection refused trying to connect to mongoDB docker instance
问题
我正在尝试部署一个带有 MongoDB 和 JPA 的简单应用程序,但无法使其正常工作。
当我将应用程序部署在 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
这是我用于构建镜像的 Dockerfile:
FROM openjdk:8-jdk-alpine
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
我用于运行 MongoDB 的 docker-compose.yml 文件如下:
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
我的所有实体都遵循以下模式(其中 <Name> 为实体的名称):
@Document
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class <Name>Entity {
@Id
private Integer <Name>Id;
// 更多数据
}
我的 JPA 存储库(与上述相同,其中 <Name> 为 JPA 类和实体的名称):
public interface <Name>JPA extends MongoRepository<<Name>Entity, Integer>{
}
最后,在 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
我可以连接到 localhost:8081 查看 mongo-express 界面,但除此之外,我没有看到其他任何信息。
英文:
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 <Name> 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 <Name> 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.
答案1
得分: 1
无法通过环回地址进行连接,因为 MongoDB 和应用位于不同的容器中。它们就像不同的机器,对于它们各自来说,localhost
不是主机,而是容器本身。请改用服务名称(来自 docker-compose.yml
):
spring.data.mongodb.host=mongo:27017
英文:
They can't connect via loopback address because mongodb and the app are in different containers. They're like like different machines and localhost
for each of them is not the host, but the container itself. Use service name (from docker-compose.yml
) instead:
spring.data.mongodb.host=mongo:27017
答案2
得分: 1
如果您试图通过本地主机与容器化的MongoDB Docker实例连接,那么您可能会遇到此问题。
解决方案:
将
mongodb://localhost:27017
替换为使用特殊DNS名称host.docker.internal连接主机:
mongodb://host.docker.internal:27017
将localhost替换为host.docker.internal即可解决问题。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论