无法使用mongo连接到docker容器
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法使用mongo连接到docker容器相关的知识,希望对你有一定的参考价值。
我有docker-compose.yml
version: '3.3'
services:
java-app:
image: java-app:latest
depends_on:
- elasticsearch
- mongo
environment:
- WAIT_HOSTS=elasticsearch:9200,mongo:27017
ports:
- "8080:8080"
elasticsearch:
image: elasticsearch:latest
command: elasticsearch
mongo_connector:
build:
./mongo-connector
depends_on:
- mongo
- elasticsearch
environment:
- WAIT_HOSTS=elasticsearch:9200,mongo:27017
mongo:
image: mongo:latest
ports:
- "27017:27017"
当我运行docker-compose.yml时,我的应用程序能够连接到Mongo实例。此外,我可以通过终端连接到此实例。
但是,默认情况下,mongo容器在没有--replSet参数的情况下启动。但我需要使用此选项运行mongo,因为mongo-connector需要启用副本集的实例。
所以我改变了docker-compose.yml:
mongo:
image: mongo:latest
entrypoint: ["mongod", "--replSet", "rs0"]
ports:
- "27017:27017"
在这种情况下,mongo容器正常启动,我在控制台中看到以下消息:
mongo_1 | 2018-03-03T12:35:27.106+0000 I NETWORK [initandlisten] waiting for connections on port 27017
尽管如此,我的应用程序无法连接到此实例。终端的连接也不起作用。如果我删除entrypoint: ["mongod", "--replSet", "rs0"]
,应用程序像往常一样连接。
你能帮帮我吗,怎么了?
UPD我已经执行了docker inspect mongo:latest
,这是输出(稍微清理一下):
[
{
"Id": "sha256:43099507792a5214bd452a0eba3482e8da488b0008525578f309f040c2f7704e",
"RepoTags": [
"mongo:latest"
],
"ContainerConfig": {
"Hostname": "a630f7a68640",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"27017/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"GOSU_VERSION=1.10",
"JSYAML_VERSION=3.10.0",
"GPG_KEYS=2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5",
"MONGO_PACKAGE=mongodb-org",
"MONGO_REPO=repo.mongodb.org",
"MONGO_MAJOR=3.6",
"MONGO_VERSION=3.6.3"
],
"Cmd": [
"/bin/sh",
"-c",
"#(nop) ",
"CMD ["mongod"]"
],
"ArgsEscaped": true,
"Volumes": {
"/data/configdb": {},
"/data/db": {}
},
"WorkingDir": "",
"Entrypoint": [
"docker-entrypoint.sh"
],
"OnBuild": [],
"Labels": {}
},
"DockerVersion": "17.06.2-ce",
"Author": "",
"Config": {
"Hostname": "",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"27017/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"GOSU_VERSION=1.10",
"JSYAML_VERSION=3.10.0",
"GPG_KEYS=2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5",
"MONGO_PACKAGE=mongodb-org",
"MONGO_REPO=repo.mongodb.org",
"MONGO_MAJOR=3.6",
"MONGO_VERSION=3.6.3"
],
"Cmd": [
"mongod"
],
"ArgsEscaped": true,
"Volumes": {
"/data/configdb": {},
"/data/db": {}
},
"WorkingDir": "",
"Entrypoint": [
"docker-entrypoint.sh"
]
},
"Architecture": "amd64",
"Os": "linux",
"Size": 365987009,
"VirtualSize": 365987009
}
]
仍然无法弄清楚如何修改默认args并传递--replSet。最初我覆盖了默认入口点,看起来这是错误的方式。
保持入口点不变并使用命令:
mongo:
image: mongo:latest
command: ["mongod", "--replSet", "rs0"]
ports:
- "27017:27017"
如果你检查Dockerfile
,你会看到它的内容如下:
ENTRYPOINT ["docker-entrypoint.sh"]
EXPOSE 27017
CMD ["mongod"]
通过替换entrypoint
,您正在替换一个300行长的bash脚本,该脚本应该根据实际命令进行一些设置,而命令最初与您的相同,只是没有额外的参数
以上是关于无法使用mongo连接到docker容器的主要内容,如果未能解决你的问题,请参考以下文章
Springboot 容器使用 docker-compose 连接到 mongo 容器的 503 错误代码
尝试从 Spring Boot Docker 容器连接到 mongo Docker 容器时验证 MongoCredential 时出错?