簡體   English   中英

無法連接到mongodb容器主機

[英]cannot connect to mongodb container host

我試圖連接到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

docker-compose.yml這一部分意味着您想將在端口27017上工作的容器在端口5100上公開給公眾,以便任何人都可以訪問localhost或public ip,但是mongodb作為主機一詞只能在容器之間識別在docker網絡內部,因此,如果您在同一網絡中有另一個容器,它可以作為主機名和端口27017到達mongodb上的mongodb

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 

因此,您需要使用--bind_ip 0.0.0.0啟動mongodb ,以便可以從容器外部訪問它。 將您的CMD更改為以下內容:

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

或者從您的Dockerfile中刪除CMD ,它會回退到基礎映像的原始CMD與上面的相同

從MongoDB 3.6開始,默認情況下mongod綁定到localhost。 請參閱默認綁定到本地主機

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM