使用docker-compose
我启动了两个安装在google vm (compute engine)
中的docker容器,其中包含一个mongodb,另一个包含一个java应用程序。应用程序很好地连接到数据库,我可以通过URL http://myvm:8080
查看结果。现在,当我想使用本地启动的相同应用程序连接同一个数据库时,我收到连接错误。
我将bind_ip属性放在0.0.0.0
以连接到远程服务器,我在日志中看到这个配置已经完成,但它不起作用。
I CONTROL [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "0.0.0.0", port: 27017 } }
可以访问VM的所有端口和协议。
在我尝试连接mongodb时遇到的错误:
com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=vm_ip:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.SocketTimeoutException: connect timed out}}]
当我通过浏览器拨打http://vm_ip:27017/
时,我会将其作为答案:
It looks like you are trying to access MongoDB over HTTP on the native driver port.
Belwo,docker-compose
档案:
version: '3'
services:
mongo:
build: ./tools/mongodb
ports:
- "27017:27017"
volumes:
- /mnt/data/mongo:/data/db
command: mongod -f /etc/mongod.conf
elasticsearch:
build: ./tools/elasticsearch
environment:
- cluster.name=resource
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
mem_limit: 1g
volumes:
- /mnt/data/elasticsearch:/usr/share/elasticsearch/data
ports:
- "9200:9200"
- "9300:9300"
tomcat:
build: ./app
ports:
- "8585:8080"
depends_on:
- mongo
- elasticsearch
volumes:
- /mnt/app/:/usr/src/app
nginx:
build: ./web
ports:
- "80:80"
volumes:
- /mnt/app/:/usr/src/app