简体   繁体   English

Docker-compose Nodejs Mongodb 不成功

[英]Docker-compose Nodejs Mongodb doesn't success

I am testing NodeJS + MongoDB on local Mac OS with docker-compose , but NodeJS and MongoDB can't connect successfully.我正在使用docker-compose在本地 Mac OS 上测试NodeJS + MongoDB ,但NodeJSMongoDB无法成功连接。


If I didn't setup --auth for MongoDB by below code, all works well.如果我没有通过以下代码为MongoDB设置--auth ,一切正常。

Here's the code:这是代码:

mongoose connection猫鼬连接

mongodb://mongodb:27017/myprojectdatabase

docker-compose.yml docker-compose.yml

version: "3"
services:
  web:
    build: .
    restart: always
    ports:
    - "8080:8080"
    depends_on:
    - mongodb
    volumes:
     - .:/mycode
  mongodb:
    image: mongo:latest
    ports:
      - "27017:27017"

Then I want to start --auth for the MongoDB like below, I got errors.然后我想像下面--auth为 MongoDB 启动--auth ,我遇到了错误。

docker-compose.yml docker-compose.yml

version: "3"
services:
  web:
    build: .
    restart: always
    ports:
    - "8080:8080"
    depends_on:
    - mongodb
    volumes:
     - .:/mycode
    # environment:
    #  - NODE_ENV=production
  mongodb:
    image: mongo:latest
    command: [--auth]
    environment:
      MONGO_INITDB_ROOT_USERNAME: my_admin
      MONGO_INITDB_ROOT_PASSWORD: my2019
      MONGO_INITDB_DATABASE: myprojectdatabase
    ports:
      - "27017:27017"
    volumes:
      - ./mydata:/data/db

Then I run然后我跑

docker-compose down -v && docker-compose up --build 

I got the output:我得到了输出:

mongodb_1  | 2019-03-01T10:54:09.847+0000 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
mongodb_1  | 2019-03-01T10:54:09.869+0000 I CONTROL  [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=9554854909b1
mongodb_1  | 2019-03-01T10:54:09.869+0000 I CONTROL  [initandlisten] db version v4.0.4
mongodb_1  | 2019-03-01T10:54:09.869+0000 I CONTROL  [initandlisten] git version: f288a3bdf201007f3693c58e140056adf8b04839
mongodb_1  | 2019-03-01T10:54:09.869+0000 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016
mongodb_1  | 2019-03-01T10:54:09.869+0000 I CONTROL  [initandlisten] allocator: tcmalloc
mongodb_1  | 2019-03-01T10:54:09.869+0000 I CONTROL  [initandlisten] modules: none
mongodb_1  | 2019-03-01T10:54:09.869+0000 I CONTROL  [initandlisten] build environment:
mongodb_1  | 2019-03-01T10:54:09.869+0000 I CONTROL  [initandlisten]     distmod: ubuntu1604
mongodb_1  | 2019-03-01T10:54:09.869+0000 I CONTROL  [initandlisten]     distarch: x86_64
mongodb_1  | 2019-03-01T10:54:09.869+0000 I CONTROL  [initandlisten]     target_arch: x86_64
mongodb_1  | 2019-03-01T10:54:09.869+0000 I CONTROL  [initandlisten] options: { net: { bindIpAll: true }, security: { authorization: "enabled" } }
mongodb_1  | 2019-03-01T10:54:09.873+0000 W STORAGE  [initandlisten] Detected unclean shutdown - /data/db/mongod.lock is not empty.
mongodb_1  | 2019-03-01T10:54:09.876+0000 I STORAGE  [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
mongodb_1  | 2019-03-01T10:54:09.878+0000 W STORAGE  [initandlisten] Recovering data from the last clean checkpoint.
mongodb_1  | 2019-03-01T10:54:09.879+0000 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=487M,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),    
web_1      | connection error: { MongoNetworkError: failed to connect to server [mongodb:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 192.168.160.2:27017]
    web_1      |     at Pool.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/topologies/server.js:505:11)
    web_1      |     at emitOne (events.js:116:13)

And some times I can see the log contains the user created information, sometimes are not.有时我可以看到日志包含用户创建的信息,有时则不是。

 2019-03-01T10:38:50.323+0000 I STORAGE  [conn2] createCollection: admin.system.users with generated UUID: 6b3b88f9-e77c-4094-a1c7-153816202a9e
mongodb_1  | Successfully added user: {
mongodb_1  |    "user" : "my_admin",
mongodb_1  |    "roles" : [
mongodb_1  |        {
mongodb_1  |            "role" : "root",
mongodb_1  |            "db" : "admin"
mongodb_1  |        }
mongodb_1  |    ]
mongodb_1  | }
mongodb_1  | 2019-03-01T10:38:50.340+0000 E -        [main] Error saving history file: FileOpenFailed: Unable to open() file /home/mongodb/.dbshell: Unknown error

I am new on docker stuff.我是码头工人的新手。 I guess the main problem is web can't establish a connection with mongodb .我想主要问题是web无法与mongodb建立连接。 Spend too long on this problem.在这个问题上花费太长时间。

Any help?有什么帮助吗? Thanks!谢谢!

Make sure you're not going to localhost of the web container.确保您不会访问 Web 容器的 localhost。 Treat containers as separate machines: localhost in one container is not shared with another one.将容器视为单独的机器:一个容器中的 localhost 不与另一个容器共享。 That's why in the connection string you have mongodb:27017 and not localhost:27017 , because mongodb in your default docker network is a DNS name of the container with mongo.这就是为什么在连接字符串中有mongodb:27017而不是localhost:27017原因,因为默认 docker 网络中的mongodb是带有 mongo 的容器的 DNS 名称。 You are using this connection string in the first (successful) case, make sure you have a valid DNS name in the second one.您在第一个(成功)案例中使用此连接字符串,请确保在第二个案例中有一个有效的 DNS 名称。

And also make sure to include your DB credentials (username:password) in the connection string too.并且还要确保在连接字符串中也包含您的数据库凭据(用户名:密码)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM