无法连接到mongodb容器主机

时间:2019-03-16 23:44:39

标签: spring mongodb docker docker-compose

im试图连接到mongodb容器,但是它不起作用,无法识别主机。

这是我的带有服务的docker-compose.yml文件:

  version: '3'
  services:

  mongodb:
    build:
      context: .
      dockerfile: docker/mongodb/Dockerfile
    restart: always
    environment:
      DATABASE: database
      USERNAME: user
      PASSWORD: pass
    ports:
      - '5100:27017'
    volumes:
      - ./data/db:/data/db

这是我的mongodb的dockerfile:

FROM mvertes/alpine-mongo:4.0.5-0

ENV MONGO_INITDB_DATABASE: ${DATABASE}
ENV MONGO_INITDB_ROOT_USERNAME: ${USERNAME}
ENV MONGO_INITDB_ROOT_PASSWORD: ${PASSWORD}

COPY ./docker/mongodb/init.js /docker-entrypoint-initdb.d

CMD ["mongod"]

EXPOSE 27017

我正在尝试从spring建立连接,但是找不到服务,这在我的属性中具有

spring.data.mongodb.host=mongodb
spring.data.mongodb.port=5100
spring.data.mongodb.username=user
spring.data.mongodb.password=pass
spring.data.mongodb.database=database

我也试图与robo3T建立连接,问题是一样的:

Address: mongodb://mongodb
port:5100

春天的错误说:

com.mongodb.MongoSocketException: mongodb: unknown name or service

在robo3T中说:

Cannot connect to the MongoDB at mongodb:5100.

Error:
Network is unreachable.

我在做什么错了?

编辑: 我的容器日志:

2019-03-16T16:22:00.714+0000 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2019-03-16T16:22:00.750+0000 I CONTROL  [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=1220eaf96d91
2019-03-16T16:22:00.750+0000 I CONTROL  [initandlisten] db version v4.0.5
2019-03-16T16:22:00.750+0000 I CONTROL  [initandlisten] git version: 3739429dd92b92d1b0ab120911a23d50bf03c412
2019-03-16T16:22:00.750+0000 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.1.1a  20 Nov 2018
2019-03-16T16:22:00.750+0000 I CONTROL  [initandlisten] allocator: system
2019-03-16T16:22:00.750+0000 I CONTROL  [initandlisten] modules: none
2019-03-16T16:22:00.750+0000 I CONTROL  [initandlisten] build environment:
2019-03-16T16:22:00.750+0000 I CONTROL  [initandlisten]     distarch: x86_64
2019-03-16T16:22:00.750+0000 I CONTROL  [initandlisten]     target_arch: x86_64
2019-03-16T16:22:00.750+0000 I CONTROL  [initandlisten] options: {}
2019-03-16T16:22:00.760+0000 I STORAGE  [initandlisten] 
2019-03-16T16:22:00.760+0000 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2019-03-16T16:22:00.760+0000 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2019-03-16T16:22:00.768+0000 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=1432M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
2019-03-16T16:22:01.523+0000 I STORAGE  [initandlisten] WiredTiger message [1552753321:523789][1:0x7f9d46a64ca8], txn-recover: Set global recovery timestamp: 0
2019-03-16T16:22:01.562+0000 I RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0)
2019-03-16T16:22:01.623+0000 I CONTROL  [initandlisten] 
2019-03-16T16:22:01.623+0000 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-03-16T16:22:01.623+0000 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-03-16T16:22:01.623+0000 I CONTROL  [initandlisten] 
2019-03-16T16:22:01.623+0000 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2019-03-16T16:22:01.623+0000 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server. 
2019-03-16T16:22:01.623+0000 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP 
2019-03-16T16:22:01.623+0000 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2019-03-16T16:22:01.623+0000 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2019-03-16T16:22:01.623+0000 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2019-03-16T16:22:01.623+0000 I CONTROL  [initandlisten] 
2019-03-16T16:22:01.638+0000 I STORAGE  [initandlisten] createCollection: admin.system.version with provided UUID: 74fe0f10-e6a8-439c-9548-a2cdd18df3c7
2019-03-16T16:22:01.688+0000 I COMMAND  [initandlisten] setting featureCompatibilityVersion to 4.0
2019-03-16T16:22:01.700+0000 I STORAGE  [initandlisten] createCollection: local.startup_log with generated UUID: 0d6bdbd7-4b15-4ad1-8ded-fa28b89ae871
2019-03-16T16:22:01.762+0000 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2019-03-16T16:22:01.765+0000 I NETWORK  [initandlisten] waiting for connections on port 27017
2019-03-16T16:22:01.825+0000 I STORAGE  [LogicalSessionCacheRefresh] createCollection: config.system.sessions with generated UUID: b5462e15-4724-4b1f-8537-57d9ebb1a2e8
2019-03-16T16:22:01.902+0000 I INDEX    [LogicalSessionCacheRefresh] build index on: config.system.sessions properties: { v: 2, key: { lastUse: 1 }, name: "lsidTTLIndex", ns: "config.system.sessions", expireAfterSeconds: 1800 }
2019-03-16T16:22:01.902+0000 I INDEX    [LogicalSessionCacheRefresh]     building index using bulk method; build may temporarily use up to 500 megabytes of RAM
2019-03-16T16:22:01.905+0000 I INDEX    [LogicalSessionCacheRefresh] build index done.  scanned 0 total records. 0 secs

1 个答案:

答案 0 :(得分:4)

docker-compose.yml的这一部分意味着您想将在端口27017上工作的容器公开给端口5100上的公共对象,以便任何人都可以通过localhost或public ip访问它,但是只能在docker网络内部的容器之间识别单词mongodb作为主机,因此,如果您在同一网络中有另一个容器,则它可以作为主机名和端口mongodb到达27017上的mongodb < / p>

ports:
  - '5100:27017'

基于mongodb日志,您遇到以下问题:

** WARNING: This server is bound to localhost. Remote systems will be unable to connect to this server. Start the server with --bind_ip <address> to specify which IP 

因此,您需要从mongodb开始--bind_ip 0.0.0.0,以便可以从容器外部访问它。将您的CMD更改为以下内容:

CMD [ "mongod", "--bind_ip", "0.0.0.0" ]

或者从您的Dockerfile中删除CMD,它将回退到基础映像的原始CMD,即the same as above

  

从MongoDB 3.6开始,默认情况下mongod绑定到localhost。参见Default Bind to Localhost