如何从Docker容器连接到localhost上的mongodb?

时间:2022-08-31 03:01:35

I'm trying to launch app with Docker-compose on windows 10 and connect to my MongoDB 3.4 running on the same machine. command to launch app: docker-compose -f src/main/docker/app.yml up

我正在尝试在Windows 10上使用Docker-compose启动应用程序并连接到在同一台机器上运行的MongoDB 3.4。命令启动应用程序:docker-compose -f src / main / docker / app.yml up

I have an application generated from jhipster and this docker config:

我有一个从jhipster和这个docker配置生成的应用程序:

version: '2'
services:
core-app:
    image: core
    environment:
        - SPRING_PROFILES_ACTIVE=prod,swagger
        - EUREKA_CLIENT_SERVICE_URL_DEFAULTZONE=http://admin:$${jhipster.registry.password}@jhipster-registry:8761/eureka
        - SPRING_CLOUD_CONFIG_URI=http://admin:$${jhipster.registry.password}@jhipster-registry:8761/config
        - SPRING_DATA_MONGODB_URI=mongodb://0.0.0.0:27017
        - SPRING_DATA_MONGODB_DATABASE=core
        - JHIPSTER_SLEEP=10 # gives time for the JHipster Registry to boot before the application
jhipster-registry:
    extends:
        file: jhipster-registry.yml
        service: jhipster-registry
    environment:
        - SPRING_CLOUD_CONFIG_SERVER_NATIVE_SEARCH_LOCATIONS=file:./central-config/docker-config/

i have an exception:

我有一个例外:

MongoSocketException: mongodb://0.0.0.0:27017}, caused by {java.net.UnknownHostException: mongodb://0.0.0.0:27017

MongoSocketException:mongodb://0.0.0.0:27017},由{java.net.UnknownHostException:mongodb://0.0.0.0:27017引起

how to fix this? Any other guides don't work

怎么解决这个问题?任何其他指南都不起作用

1 个解决方案

#1


0  

If you are running Mongo without going through docker/docker-compose, its hosted on your localhost.

如果您在没有经过docker / docker-compose的情况下运行Mongo,它将托管在您的localhost上。

You cant really reveal localhost from host to a docker container just like that (without using network --host option which isn't recommended).

你不能将localhost从主机显示到docker容器就好(不建议使用network --host选项)。

I would suggest you create a service for mongodb inside docker-compose file and link it to your main service.

我建议你在docker-compose文件中为mongodb创建一个服务,并将它链接到你的主服务。

#1


0  

If you are running Mongo without going through docker/docker-compose, its hosted on your localhost.

如果您在没有经过docker / docker-compose的情况下运行Mongo,它将托管在您的localhost上。

You cant really reveal localhost from host to a docker container just like that (without using network --host option which isn't recommended).

你不能将localhost从主机显示到docker容器就好(不建议使用network --host选项)。

I would suggest you create a service for mongodb inside docker-compose file and link it to your main service.

我建议你在docker-compose文件中为mongodb创建一个服务,并将它链接到你的主服务。