docker-library / docs

Documentation for Docker Official Images in docker-library
https://github.com/docker-library/official-images
MIT License
5.08k stars 2.21k forks source link

Node cannot connect to mongodb #1409

Closed HerryLo closed 5 years ago

HerryLo commented 5 years ago

Dockerfileconfig file

# 基于最新的 node 镜像
FROM node:8.11.1
# 复制当前目录下所有文件到目标镜像 /app/ 目录下
COPY . /usr/src/app/
# 修改工作目录
WORKDIR /usr/src/app/
# yarn 一下,安装依赖
RUN ["yarn", "install"]
# 端口号
EXPOSE 12345
# 启动 node server
ENTRYPOINT ["npm", "run", "dev"]

docker-compose.yml 文件

version: '3'
services:
  mongo:
    image: mongo
    volumes:
      - "./data/:/data/db"
    ports:
      - 27017:27017
    command: ["mongod"]

  node:
    # build构建 本地Dockerfile文件
    build: .
    ports:
      - 12345:12345
    # depends_on:
    #   - mongo
    links:
      - mongo

cmd

Building node
Step 1/6 : FROM node:8.11.1
 ---> 78f8aef50581
Step 2/6 : COPY . /usr/src/app/
 ---> 68313bcab056
Step 3/6 : WORKDIR /usr/src/app/
Removing intermediate container b7bfc8b60b95
 ---> 4e840e97365b
Step 4/6 : RUN ["yarn", "install"]
 ---> Running in aa2e45b32559
yarn install v1.5.1
[1/4] Resolving packages...
[2/4] Fetching packages...
info fsevents@1.2.4: The platform "linux" is incompatible with this module.
info "fsevents@1.2.4" is an optional dependency and failed compatibility check. Excluding it from installation.
[3/4] Linking dependencies...
[4/4] Building fresh packages...
Done in 17.89s.
Removing intermediate container aa2e45b32559
 ---> ad70304a48bb
Step 5/6 : EXPOSE 12345
 ---> Running in 9e4042b94f52
Removing intermediate container 9e4042b94f52
 ---> b9e47f72d26d
Step 6/6 : ENTRYPOINT ["npm", "run", "dev"]
 ---> Running in 238dcca69b3c
Removing intermediate container 238dcca69b3c
 ---> 9f9c945000d5
Successfully built 9f9c945000d5
Successfully tagged koa-mongodb_node:latest
Creating koa-mongodb_mongo_1 ... done
Creating koa-mongodb_node_1  ... done
Attaching to koa-mongodb_mongo_1, koa-mongodb_node_1
mongo_1  | 2019-01-24T02:41:19.889+0000 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
mongo_1  | 2019-01-24T02:41:19.899+0000 I CONTROL  [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=760d53cbc3e0
mongo_1  | 2019-01-24T02:41:19.899+0000 I CONTROL  [initandlisten] db version v4.0.5
mongo_1  | 2019-01-24T02:41:19.899+0000 I CONTROL  [initandlisten] git version: 3739429dd92b92d1b0ab120911a23d50bf03c412
mongo_1  | 2019-01-24T02:41:19.899+0000 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016
mongo_1  | 2019-01-24T02:41:19.899+0000 I CONTROL  [initandlisten] allocator: tcmalloc
mongo_1  | 2019-01-24T02:41:19.900+0000 I CONTROL  [initandlisten] modules: none
mongo_1  | 2019-01-24T02:41:19.900+0000 I CONTROL  [initandlisten] build environment:
mongo_1  | 2019-01-24T02:41:19.900+0000 I CONTROL  [initandlisten]     distmod: ubuntu1604
mongo_1  | 2019-01-24T02:41:19.900+0000 I CONTROL  [initandlisten]     distarch: x86_64
mongo_1  | 2019-01-24T02:41:19.900+0000 I CONTROL  [initandlisten]     target_arch: x86_64
mongo_1  | 2019-01-24T02:41:19.900+0000 I CONTROL  [initandlisten] options: { net: { bindIpAll: true } }
mongo_1  | 2019-01-24T02:41:19.908+0000 I STORAGE  [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
mongo_1  | 2019-01-24T02:41:19.914+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),
node_1   |
node_1   | > yes@1.0.0 dev /usr/src/app
node_1   | > cross-env NODE_ENV=development supervisor --harmony ./server/index.js
node_1   |
node_1   |
node_1   | Running node-supervisor with
node_1   |   program '--harmony ./server/index.js'
node_1   |   --watch '.'
node_1   |   --extensions 'node,js'
node_1   |   --exec 'node'
node_1   |
node_1   | Starting child process with 'node --harmony ./server/index.js'
node_1   | Watching directory '/usr/src/app' for changes.
node_1   | Press rs for restarting the process.
node_1   | Thu, 24 Jan 2019 02:41:24 GMT koa deprecated Support for generators will be removed in v3. See the documentationfor examples of how to convert old middleware https://github.com/koajs/koa/blob/master/docs/migration.md at server/app.js:71:5
node_1   | Node Server open: localhost:12345
node_1   | connection error: { MongoNetworkError: failed to connect to server [172.18.0.2:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 172.18.0.2:27017]
node_1   |     at Pool.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/topologies/server.js:564:11)
node_1   |     at emitOne (events.js:116:13)
node_1   |     at Pool.emit (events.js:211:7)
node_1   |     at Connection.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/connection/pool.js:317:12)
node_1   |     at Object.onceWrapper (events.js:317:30)
node_1   |     at emitTwo (events.js:126:13)
node_1   |     at Connection.emit (events.js:214:7)
node_1   |     at Socket.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/connection/connection.js:246:50)
node_1   |     at Object.onceWrapper (events.js:315:30)
node_1   |     at emitOne (events.js:116:13)
node_1   |     at Socket.emit (events.js:211:7)
node_1   |     at emitErrorNT (internal/streams/destroy.js:64:8)
node_1   |     at _combinedTickCallback (internal/process/next_tick.js:138:11)
node_1   |     at process._tickCallback (internal/process/next_tick.js:180:9)
node_1   |   name: 'MongoNetworkError',
node_1   |   errorLabels: [ 'TransientTransactionError' ],
node_1   |   [Symbol(mongoErrorContextSymbol)]: {} }
mongo_1  | 2019-01-24T02:41:31.188+0000 I STORAGE  [initandlisten] WiredTiger message [1548297691:188270][1:0x7f1ff0889a40], txn-recover: Main recovery loop: starting at 9/7552 to 10/256
mongo_1  | 2019-01-24T02:41:31.898+0000 I STORAGE  [initandlisten] WiredTiger message [1548297691:898184][1:0x7f1ff0889a40], txn-recover: Recovering log 9 through 10
mongo_1  | 2019-01-24T02:41:32.412+0000 I STORAGE  [initandlisten] WiredTiger message [1548297692:412796][1:0x7f1ff0889a40], txn-recover: Recovering log 10 through 10
mongo_1  | 2019-01-24T02:41:32.475+0000 I STORAGE  [initandlisten] WiredTiger message [1548297692:475213][1:0x7f1ff0889a40], txn-recover: Set global recovery timestamp: 0
mongo_1  | 2019-01-24T02:41:32.510+0000 I RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0)
mongo_1  | 2019-01-24T02:41:32.570+0000 I CONTROL  [initandlisten]
mongo_1  | 2019-01-24T02:41:32.570+0000 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
mongo_1  | 2019-01-24T02:41:32.570+0000 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
mongo_1  | 2019-01-24T02:41:32.570+0000 I CONTROL  [initandlisten]
mongo_1  | 2019-01-24T02:41:32.648+0000 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
mongo_1  | 2019-01-24T02:41:32.655+0000 I NETWORK  [initandlisten] waiting for connections on port 27017

But it's possible to connect mongodb locally. npm run dev connected true。There's a problem with node docker container connections.

what???

wglambert commented 5 years ago

Looks like your Node container is trying to connect to Mongo while it's still initializing and before it's started accepting connections

node_1 | connection error: { MongoNetworkError: failed to connect to server [172.18.0.2:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 172.18.0.2:27017] . . . mongo_1 | 2019-01-24T02:41:32.655+0000 I NETWORK [initandlisten] waiting for connections on port 27017

tianon commented 5 years ago

In the future, these sorts of questions/requests would be more appropriately posted to the Docker Community Forums, the Docker Community Slack, or Stack Overflow.