无法连接到运行在计算引擎上的docker容器上的mongodb

使用docker-compose我启动了两个docker容器,一个是包含mongodb的第二个包含java应用程序的google vm (compute engine) 。 该应用程序很好地连接到数据库,我可以通过URL http://myvm:8080查看结果。 现在,当我想要在本地启动相同的应用程序连接到相同的数据库时,出现连接错误。

我把bind_ip属性设置为0.0.0.0来连接远程服务器,我在日志中看到这个configuration已经被使用了,但是不起作用。

 I CONTROL [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "0.0.0.0", port: 27017 } } 

虚拟机的所有端口和协议都可以访问。

在我尝试在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. 

贝沃, 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