Docker-compose Nodejs Mongodb doesn't success - node.js

I am testing NodeJS + MongoDB on local Mac OS with docker-compose, but NodeJS and MongoDB can't connect successfully.
If I didn't setup --auth for MongoDB by below code, all works well.
Here's the code:
mongoose connection
mongodb://mongodb:27017/myprojectdatabase
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.
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. Spend too long on this problem.
Any help? Thanks!

Make sure you're not going to localhost of the web container. Treat containers as separate machines: localhost in one container is not shared with another one. 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. You are using this connection string in the first (successful) case, make sure you have a valid DNS name in the second one.
And also make sure to include your DB credentials (username:password) in the connection string too.

Related

cannot connect to MongoDB with Docker-compose

I'm trying to create 2 containers with Docker-compose which one contain Mongo serive and another contain web service written by NodeJS.
This is my docker-compose.yml file:
version: '3'
services:
mongo:
build: ./docker_mongo/
volumes:
- ./database:/data/db
ports:
- "27017:27017"
node_app:
build: ./docker_node/
ports:
- "3001:3001"
- "16427:16427"
command: npm start
environment:
- OS:PI
depends_on:
- mongo
- mqtt
When I ran docker-compose, mongo service display this message:
mongo_1 | 2020-01-05T07:36:39.699+0000 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
mongo_1 | 2020-01-05T07:36:39.703+0000 I CONTROL [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=90cbb91eebd0
mongo_1 | 2020-01-05T07:36:39.703+0000 I CONTROL [initandlisten] db version v4.0.14
mongo_1 | 2020-01-05T07:36:39.703+0000 I CONTROL [initandlisten] git version: 1622021384533dade8b3c89ed3ecd80e1142c132
mongo_1 | 2020-01-05T07:36:39.703+0000 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2g 1 Mar 2016
mongo_1 | 2020-01-05T07:36:39.703+0000 I CONTROL [initandlisten] allocator: tcmalloc
mongo_1 | 2020-01-05T07:36:39.703+0000 I CONTROL [initandlisten] modules: none
mongo_1 | 2020-01-05T07:36:39.703+0000 I CONTROL [initandlisten] build environment:
mongo_1 | 2020-01-05T07:36:39.703+0000 I CONTROL [initandlisten] distmod: ubuntu1604
mongo_1 | 2020-01-05T07:36:39.703+0000 I CONTROL [initandlisten] distarch: x86_64
mongo_1 | 2020-01-05T07:36:39.703+0000 I CONTROL [initandlisten] target_arch: x86_64
mongo_1 | 2020-01-05T07:36:39.703+0000 I CONTROL [initandlisten] options: {}
mongo_1 | 2020-01-05T07:36:39.708+0000 I STORAGE [initandlisten]
mongo_1 | 2020-01-05T07:36:39.708+0000 I STORAGE [initandlisten] ** WARNING: Support for MMAPV1 storage engine has been deprecated and will be
mongo_1 | 2020-01-05T07:36:39.708+0000 I STORAGE [initandlisten] ** removed in version 4.2. Please plan to migrate to the wiredTiger
mongo_1 | 2020-01-05T07:36:39.708+0000 I STORAGE [initandlisten] ** storage engine.
mongo_1 | 2020-01-05T07:36:39.708+0000 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/deprecated-mmapv1
mongo_1 | 2020-01-05T07:36:39.708+0000 I STORAGE [initandlisten]
mongo_1 | 2020-01-05T07:36:39.708+0000 I STORAGE [initandlisten] Detected data files in /data/db created by the 'mmapv1' storage engine, so setting the active storage engine to 'mmapv1'.
mongo_1 | 2020-01-05T07:36:39.727+0000 I JOURNAL [initandlisten] journal dir=/data/db/journal
mongo_1 | 2020-01-05T07:36:39.728+0000 I JOURNAL [initandlisten] recover : no journal files present, no recovery needed
mongo_1 | 2020-01-05T07:36:39.730+0000 I STORAGE [initandlisten] WARNING: This file system is not supported. For further information see:
mongo_1 | 2020-01-05T07:36:39.730+0000 I STORAGE [initandlisten] http://dochub.mongodb.org/core/unsupported-filesystems
mongo_1 | 2020-01-05T07:36:39.730+0000 I STORAGE [initandlisten] Please notify MongoDB, Inc. if an unlisted filesystem generated this warning.
mongo_1 | 2020-01-05T07:36:39.970+0000 I JOURNAL [durability] Durability thread started
mongo_1 | 2020-01-05T07:36:39.971+0000 I JOURNAL [journal writer] Journal writer thread started
mongo_1 | 2020-01-05T07:36:40.003+0000 I STORAGE [initandlisten] WARNING: This file system is not supported. For further information see:
mongo_1 | 2020-01-05T07:36:40.003+0000 I STORAGE [initandlisten] http://dochub.mongodb.org/core/unsupported-filesystems
mongo_1 | 2020-01-05T07:36:40.003+0000 I STORAGE [initandlisten] Please notify MongoDB, Inc. if an unlisted filesystem generated this warning.
mongo_1 | 2020-01-05T07:36:40.003+0000 I CONTROL [initandlisten]
mongo_1 | 2020-01-05T07:36:40.003+0000 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
mongo_1 | 2020-01-05T07:36:40.003+0000 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
mongo_1 | 2020-01-05T07:36:40.003+0000 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
mongo_1 | 2020-01-05T07:36:40.003+0000 I CONTROL [initandlisten]
mongo_1 | 2020-01-05T07:36:40.003+0000 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost.
mongo_1 | 2020-01-05T07:36:40.003+0000 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server.
mongo_1 | 2020-01-05T07:36:40.003+0000 I CONTROL [initandlisten] ** Start the server with --bind_ip <address> to specify which IP
mongo_1 | 2020-01-05T07:36:40.003+0000 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to
mongo_1 | 2020-01-05T07:36:40.003+0000 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the
mongo_1 | 2020-01-05T07:36:40.003+0000 I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning.
mongo_1 | 2020-01-05T07:36:40.003+0000 I CONTROL [initandlisten]
mongo_1 | 2020-01-05T07:36:40.308+0000 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
mongo_1 | 2020-01-05T07:36:40.312+0000 I NETWORK [initandlisten] waiting for connections on port 27017
This shows that mongo service is ready on port 27017, but NodeJs server still could not to database:
node_app_1 | Error: Unable to connect to database
node_app_1 | MongoTimeoutError: Server selection timed out after 30000 ms
node_app_1 | at /node_app/app.js:84:20
With NodeJs server, mongoDb connect to url mongodb://mongo:27017/docker with mongo is the name of docker-compose service. I also tried with localhost but it didn't work.
I've tried this on a raspberry Pi, and it worked (just change mongo image).
Mongo Image on Pi
Can someone help?
In order to connect two containers, first, you have to understand docker networking. You are getting error probably because containers are isolated. There are multiple ways you can connect docker containers using different networking mode like:
Host-only network
Bridge networking
Read more here https://docs.docker.com/network/
You can configure host-only network as below:
Include network_mode: host for each service.
version: '3'
services:
mongo:
build: ./docker_mongo/
network_mode: host
volumes:
- ./database:/data/db
ports:
- "27017:27017"
node_app:
build: ./docker_node/
network_mode: host
ports:
- "3001:3001"
- "16427:16427"
command: npm start
environment:
- OS:PI
depends_on:
- mongo
- mqtt

How to configure Mongodb connection within kubernetes to connect with my nodejs app

I create an nodejs app and try to connect it with mongodb on kubernetes cluster. The nodejs and mongodb app are separate pods in my cluster.
mongodb and app are running when i display the status , i can connect me to the mongodb pods and add datas
NAME READY STATUS RESTARTS AGE
my-backend-core-test-5d7b78c9dc-dt4bg 1/1 Running 0 31m
my-frontend-test-6868f7c7dd-b2qtm 1/1 Running 0 40h
my-mongodb-test-7d55dbff74-2m6cm 1/1 Running 0 34m
But when i try to make the connection with this script:
const urlDB = "my-mongodb-service-test.my-test.svc.cluster.local:27017";
console.log("urlDB :: ", urlDB);
mongoose.connect('mongodb://'+urlDB+'/test', { useNewUrlParser: true }).then(() => {
console.log("DB connected")
}).catch((err)=> {
console.log("ERROR")
})
I have the following error on my nodejs app:
> my-core#1.0.0 start /usr/src/app
> node ./src/app.js
urlDB :: my-mongodb-service-test.my-test.svc.cluster.local:27017
ERROR
As explained on kubernetes i'm suppose to communicate between the differents pods using service-name.namespace.svc.cluster.local (my-mongodb-service-test.my-test.svc.cluster.local:27017)
mongo logs show me a different host, corresponding to my pod and not the service. How can i configure the host on my yaml file ?
mongodb logs :
2019-05-24T10:57:02.367+0000 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2019-05-24T10:57:02.374+0000 I CONTROL [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=my-mongodb-test-7d55dbff74-2m6cm
2019-05-24T10:57:02.374+0000 I CONTROL [initandlisten] db version v4.0.9
2019-05-24T10:57:02.374+0000 I CONTROL [initandlisten] git version: fc525e2d9b0e4bceff5c2201457e564362909765
2019-05-24T10:57:02.374+0000 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2g 1 Mar 2016
2019-05-24T10:57:02.375+0000 I CONTROL [initandlisten] allocator: tcmalloc
2019-05-24T10:57:02.375+0000 I CONTROL [initandlisten] modules: none
2019-05-24T10:57:02.375+0000 I CONTROL [initandlisten] build environment:
2019-05-24T10:57:02.375+0000 I CONTROL [initandlisten] distmod: ubuntu1604
2019-05-24T10:57:02.375+0000 I CONTROL [initandlisten] distarch: x86_64
2019-05-24T10:57:02.375+0000 I CONTROL [initandlisten] target_arch: x86_64
2019-05-24T10:57:02.375+0000 I CONTROL [initandlisten] options: { net: { bindIp: "0.0.0.0" } }
2019-05-24T10:57:02.376+0000 I STORAGE [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2019-05-24T10:57:02.377+0000 I STORAGE [initandlisten]
2019-05-24T10:57:02.377+0000 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2019-05-24T10:57:02.377+0000 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2019-05-24T10:57:02.377+0000 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=485M,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-05-24T10:57:03.521+0000 I STORAGE [initandlisten] WiredTiger message [1558695423:521941][1:0x7f2d2eeb0a80], txn-recover: Main recovery loop: starting at 2/140416 to 3/256
2019-05-24T10:57:03.719+0000 I STORAGE [initandlisten] WiredTiger message [1558695423:719280][1:0x7f2d2eeb0a80], txn-recover: Recovering log 2 through 3
2019-05-24T10:57:03.836+0000 I STORAGE [initandlisten] WiredTiger message [1558695423:836203][1:0x7f2d2eeb0a80], txn-recover: Recovering log 3 through 3
2019-05-24T10:57:03.896+0000 I STORAGE [initandlisten] WiredTiger message [1558695423:896185][1:0x7f2d2eeb0a80], txn-recover: Set global recovery timestamp: 0
2019-05-24T10:57:03.924+0000 I RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0)
2019-05-24T10:57:03.947+0000 I CONTROL [initandlisten]
2019-05-24T10:57:03.947+0000 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-05-24T10:57:03.947+0000 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2019-05-24T10:57:03.947+0000 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2019-05-24T10:57:03.947+0000 I CONTROL [initandlisten]
2019-05-24T10:57:03.947+0000 I CONTROL [initandlisten]
2019-05-24T10:57:03.947+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-05-24T10:57:03.947+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-05-24T10:57:03.947+0000 I CONTROL [initandlisten]
2019-05-24T10:57:03.984+0000 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2019-05-24T10:57:03.986+0000 I NETWORK [initandlisten] waiting for connections on port 27017
mongodb yaml
apiVersion: v1
kind: Service
metadata:
name: my-mongodb-service-test
namespace: my-test
spec:
selector:
app: my-mongodb
env: test
ports:
- port: 27017
targetPort: 27017
protocol: TCP
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-mongodb-test
namespace: my-test
labels:
app: my-mongodb
env: test
spec:
selector:
matchLabels:
app: my-mongodb-test
replicas: 1
template:
metadata:
labels:
app: my-mongodb-test
spec:
containers:
- name: mongo
image: mongo:4.0.9
command:
- mongod
- "--bind_ip"
- "0.0.0.0"
imagePullPolicy: Always
ports:
- containerPort: 27017
name: mongo
hostPort: 27017
protocol: TCP
volumeMounts:
- mountPath: /data/db
name: mongodb-volume
volumes:
- name: mongodb-volume
hostPath:
path: /home/debian/mongodb
Your service selector is mismatch with pod labels, service endpoints is empty (you can check this by kubectl describe svc/my-mongodb-service-test -n my-test), so kubernetes can not access pod by service.
Correct service selector is:
apiVersion: v1
kind: Service
metadata:
name: my-mongodb-service-test
namespace: my-test
spec:
selector:
app: my-mongodb
...
This should match pod labels specify by spec.template.metadata.labels in Deployment yaml.

Code 14 when Dockerizing Web App Using node.js and MongoDB

I am trying to Dockerize a web app I built using node.js and MongoDB, but when I run
docker-compose up
I am getting the following error:
$ docker-compose up
Creating network "nodeapp1_default" with the default driver Creating
mongo ... done Creating app ... done Attaching to mongo, app mongo
| 2019-02-05T15:02:34.925+0000 I CONTROL [main] Automatically
disabling TLS 1.0, to force-enable TLS 1.0 specify
--sslDisabledProtocols 'none' mongo | 2019-02-05T15:02:34.934+0000 I CONTROL [initandlisten] MongoDB starting : pid=1 port=27017
dbpath=/data/db 64-bit host=e4fb5b7aa837 mongo |
2019-02-05T15:02:34.934+0000 I CONTROL [initandlisten] db version
v4.0.5 mongo | 2019-02-05T15:02:34.935+0000 I CONTROL
[initandlisten] git version: 3739429dd92b92d1b0ab120911a23d50bf03c412
mongo | 2019-02-05T15:02:34.935+0000 I CONTROL [initandlisten]
OpenSSL version: OpenSSL 1.0.2g 1 Mar 2016 mongo |
2019-02-05T15:02:34.935+0000 I CONTROL [initandlisten] allocator:
tcmalloc mongo | 2019-02-05T15:02:34.936+0000 I CONTROL
[initandlisten] modules: none mongo | 2019-02-05T15:02:34.936+0000
I CONTROL [initandlisten] build environment: mongo |
2019-02-05T15:02:34.936+0000 I CONTROL [initandlisten] distmod:
ubuntu1604 mongo | 2019-02-05T15:02:34.937+0000 I CONTROL
[initandlisten] distarch: x86_64 mongo |
2019-02-05T15:02:34.937+0000 I CONTROL [initandlisten]
target_arch: x86_64 mongo | 2019-02-05T15:02:34.937+0000 I CONTROL
[initandlisten] options: { net: { bindIpAll: true } } mongo |
2019-02-05T15:02:34.941+0000 I STORAGE [initandlisten]
wiredtiger_open config:
create,cache_size=256M,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),
mongo | 2019-02-05T15:02:36.060+0000 E STORAGE [initandlisten]
WiredTiger error (17) [1549378956:60130][1:0x7f0c375e0a40],
connection: __posix_open_file, 715: /data/db/WiredTiger.wt:
handle-open: open: File exists Raw:
[1549378956:60130][1:0x7f0c375e0a40], connection: __posix_open_file,
715: /data/db/WiredTiger.wt: handle-open: open: File exists mongo |
2019-02-05T15:02:36.066+0000 E STORAGE [initandlisten] WiredTiger
error (26) [1549378956:66441][1:0x7f0c375e0a40], connection:
__posix_fs_rename, 253: /data/db/WiredTiger.wt to /data/db/WiredTiger.wt.1: file-rename: rename: Text file busy Raw:
[1549378956:66441][1:0x7f0c375e0a40], connection: __posix_fs_rename,
253: /data/db/WiredTiger.wt to /data/db/WiredTiger.wt.1: file-rename:
rename: Text file busy mongo | 2019-02-05T15:02:36.077+0000 E
STORAGE [initandlisten] WiredTiger error (17)
[1549378956:77316][1:0x7f0c375e0a40], connection: __posix_open_file,
715: /data/db/WiredTiger.wt: handle-open: open: File exists Raw:
[1549378956:77316][1:0x7f0c375e0a40], connection: __posix_open_file,
715: /data/db/WiredTiger.wt: handle-open: open: File exists mongo |
2019-02-05T15:02:36.078+0000 E STORAGE [initandlisten] WiredTiger
error (26) [1549378956:78533][1:0x7f0c375e0a40], connection:
__posix_fs_rename, 253: /data/db/WiredTiger.wt to /data/db/WiredTiger.wt.1: file-rename: rename: Text file busy Raw:
[1549378956:78533][1:0x7f0c375e0a40], connection: __posix_fs_rename,
253: /data/db/WiredTiger.wt to /data/db/WiredTiger.wt.1: file-rename:
rename: Text file busy mongo | 2019-02-05T15:02:36.090+0000 E
STORAGE [initandlisten] WiredTiger error (17)
[1549378956:90882][1:0x7f0c375e0a40], connection: __posix_open_file,
715: /data/db/WiredTiger.wt: handle-open: open: File exists Raw:
[1549378956:90882][1:0x7f0c375e0a40], connection: __posix_open_file,
715: /data/db/WiredTiger.wt: handle-open: open: File exists mongo |
2019-02-05T15:02:36.092+0000 E STORAGE [initandlisten] WiredTiger
error (26) [1549378956:92202][1:0x7f0c375e0a40], connection:
__posix_fs_rename, 253: /data/db/WiredTiger.wt to /data/db/WiredTiger.wt.1: file-rename: rename: Text file busy Raw:
[1549378956:92202][1:0x7f0c375e0a40], connection: __posix_fs_rename,
253: /data/db/WiredTiger.wt to /data/db/WiredTiger.wt.1: file-rename:
rename: Text file busy mongo | 2019-02-05T15:02:36.094+0000 W
STORAGE [initandlisten] Failed to start up WiredTiger under any
compatibility version. mongo | 2019-02-05T15:02:36.094+0000 F
STORAGE [initandlisten] Reason: 26: Text file busy mongo |
2019-02-05T15:02:36.094+0000 F - [initandlisten] Fatal
Assertion 28595 at
src/mongo/db/storage/wiredtiger/wiredtiger_kv_engine.cpp 638 mongo
| 2019-02-05T15:02:36.094+0000 F - [initandlisten] mongo |
mongo | ***aborting after fassert() failure mongo | mongo |
mongo exited with code 14
Here is my Dockerfile:
FROM node:8
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000 CMD [ "npm", "start" ]
and my docker-compose.yml file:
version: "2" services: app:
container_name: app
restart: always
build: .
ports:
- "3000:3000"
links:
- mongo mongo:
container_name: mongo
image: mongo
volumes:
- ./data:/data/db
ports:
- "27017:27017"
The problem seems to be coming from some file called WhiteTiger.wt. I have tried erasing this file from the data directory of my project directory but that did not work. Has anyone come across this problem before?
Thank you
I am assuming you are running Docker on Windows, and if so, I experienced the same issue and found the answer in the below post:
Windows Docker mongo container doesn't work with volume mount
The problem is that the volume mount is specified as a host volume. I resolved my issue by changing my volume mount to be a named volume. If you need to use host, you might be able to use the rsync tool specified in the answer to the question I linked.
My docker-compose.yml file
version: '3'
services:
mongodb1:
image: mongo:latest
restart: always
volumes:
- data1:/data/db
- config1:/data/configdb
ports:
- 30001:27017
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
volumes:
data1:
config1:

Mongodb connection error inside docker container

I've been trying to get a basic nodeJS api to connect to a mongo container. Both services are defined in a docker-compose.yml file. I've read countless similar questions here and on docker's forum all stating that the issue is your mongo connection URI. This is not my issue as you'll see below.
docker-compose.yml
version: '3.7'
services:
api:
build: ./
command: npm run start:dev
working_dir: /usr/src/api-boiler/
restart: always
environment:
PORT: 3001
MONGODB_URI: mongodb://mongodb:27017/TodoApp
JWT_SECRET: asdkasd9a9sdn2r3513032
links:
- mongodb
ports:
- "3001:3001"
volumes:
- ./:/usr/src/api-boiler/
depends_on:
- mongodb
mongodb:
image: mongo
restart: always
volumes:
- /usr/local/var/mongodb:/data/db
ports:
- 27017:27017
Dockerfile
FROM node:10.8.0
WORKDIR /usr/src/api-boiler
COPY ./ ./
RUN npm install
CMD ["/bin/bash"]
db/mongoose.js
Setting up mongodb connection
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect(
process.env.MONGODB_URI,
{ useMongoClient: true }
);
module.exports.mongoose = mongoose;
But no matter what the api container cannot connect. I'm tried setting the mongo uri to 0.0.0.0:3001 but no joy. I checked the config settings used to launch mongo in the container using db.serverCmdLineOpts(). And, the command bind_ip_all has been passed so mongo should accept connections from any ip. The typical issue is people forgetting to replace localhost with their mongo container name. EG:
mongodb://localhost:27017/TodoApp >> mongodb://mongodb:27017/TodoApp
But, this has been done. So pretty stumped.
Logs - for good measure
Successfully built 388868008521
Successfully tagged api-boiler_api:latest
Starting api-boiler_mongodb_1 ... done
Recreating api-boiler_api_1 ... done
Attaching to api-boiler_mongodb_1, api-boiler_api_1
mongodb_1 | 2018-08-20T20:09:27.072+0000 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify -- sslDisabledProtocols 'none'
mongodb_1 | 2018-08-20T20:09:27.085+0000 I CONTROL [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=72af162616c8
mongodb_1 | 2018-08-20T20:09:27.085+0000 I CONTROL [initandlisten] db version v4.0.1
mongodb_1 | 2018-08-20T20:09:27.085+0000 I CONTROL [initandlisten] git version: 54f1582fc6eb01de4d4c42f26fc133e623f065fb
mongodb_1 | 2018-08-20T20:09:27.085+0000 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2g 1 Mar 2016
mongodb_1 | 2018-08-20T20:09:27.085+0000 I CONTROL [initandlisten] allocator: tcmalloc
mongodb_1 | 2018-08-20T20:09:27.085+0000 I CONTROL [initandlisten] modules: none
mongodb_1 | 2018-08-20T20:09:27.085+0000 I CONTROL [initandlisten] build environment:
mongodb_1 | 2018-08-20T20:09:27.085+0000 I CONTROL [initandlisten] distmod: ubuntu1604
mongodb_1 | 2018-08-20T20:09:27.085+0000 I CONTROL [initandlisten] distarch: x86_64
mongodb_1 | 2018-08-20T20:09:27.085+0000 I CONTROL [initandlisten] target_arch: x86_64
mongodb_1 | 2018-08-20T20:09:27.085+0000 I CONTROL [initandlisten] options: { net: { bindIpAll: true } }
mongodb_1 | 2018-08-20T20:09:27.088+0000 W STORAGE [initandlisten] Detected unclean shutdown - /data/db/mongod.lock is not empty.
mongodb_1 | 2018-08-20T20:09:27.093+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 | 2018-08-20T20:09:27.096+0000 W STORAGE [initandlisten] Recovering data from the last clean checkpoint.
mongodb_1 | 2018-08-20T20:09:27.097+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),
api_1 |
api_1 | > api-boiler#0.1.0 start:dev /usr/src/api-boiler
api_1 | > cross-env NODE_ENV=development node server/server.js
api_1 |
api_1 | Started on port 3001
api_1 | (node:24) UnhandledPromiseRejectionWarning: MongoError: failed to connect to server [mongodb:27017] on first connect [MongoError: connect ECONNREFUSED 172.18.0.2:27017]
OK. I've solved it. With the help of this blog here - https://dev.to/hugodias/wait-for-mongodb-to-start-on-docker-3h8b
You need to wait for mongod to fully start inside the container. The depend_on key in docker-compose.yml is not sufficient.
You'll also need to update your Dockerfile to take advantage of docker-compose-wait.
For reference - here is my updated docker-compose and Dockerfile files.
version: '3.7'
services:
api:
build: ./
working_dir: /usr/src/api-boiler/
restart: always
environment:
PORT: 3001
MONGODB_URI: mongodb://mongodb:27017/TodoApp
JWT_SECRET: asdkasd9a9sdn2r3513032
ports:
- "3001:3001"
volumes:
- ./:/usr/src/api-boiler/
depends_on:
- mongodb
environment:
WAIT_HOSTS: mongodb:27017
mongodb:
image: mongo
container_name: mongodb
restart: always
volumes:
- 27017:27017
FROM node:10.8.0
WORKDIR /usr/src/api-boiler
COPY ./ ./
RUN npm install
EXPOSE 3001
## THE LIFE SAVER
ADD https://github.com/ufoscout/docker-compose- wait/releases/download/2.2.1/wait /wait
RUN chmod +x /wait
# CMD ["/bin/bash"]
CMD /wait && npm run start:dev
I had a similar issue and the cause was that despite I explicitly pointed that backend depends_on database it was not enough and backend was starting earlier than my database was fully ready.
This is what helped:
backend:
depends_on:
db:
condition: service_healthy
db:
...
healthcheck:
test: echo 'db.runCommand("ping").ok' | mongosh db:27017 --quiet
interval: 10s
timeout: 10s
retries: 5
start_period: 40s
This way backend service isn't started until healthcheck is passed

docker Error while trying to connect to mongodb

I want to make simple restful API. I am using docker to do this. Here is my Dockerfile:
FROM mongo:3.2
EXPOSE 3000
RUN apt-get update; apt-get install curl -y
RUN curl -sL https://deb.nodesource.com/setup_6.x | bash -
RUN apt-get install -y nodejs
ADD . .
CMD node app.js
My docker-compose.yml looks like this:
version: '2'
services:
db:
build: ../images/mongodb
ports:
- "27017:27017"
- "3000:3000"
my app.js file looks like this:
var express = require('express')
var app = express()
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://127.0.0.1:27017/sample');
app.get('/', function (req, res) {
res.send('Hello World!')
})
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
When run docker-compose up --build, I am having and error:
db_1 | Example app listening on port 3000!
db_1 |
db_1 | events.js:160
db_1 | throw er; // Unhandled 'error' event
db_1 | ^
db_1 | MongoError: failed to connect to server [127.0.0.1:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
db_1 | at Pool.<anonymous> (/node_modules/mongodb-core/lib/topologies/server.js:327:35)
db_1 | at emitOne (events.js:96:13)
db_1 | at Pool.emit (events.js:188:7)
db_1 | at Connection.<anonymous> (/node_modules/mongodb-core/lib/connection/pool.js:274:12)
db_1 | at Connection.g (events.js:291:16)
db_1 | at emitTwo (events.js:106:13)
db_1 | at Connection.emit (events.js:191:7)
db_1 | at Socket.<anonymous> (/node_modules/mongodb-core/lib/connection/connection.js:177:49)
db_1 | at Socket.g (events.js:291:16)
db_1 | at emitOne (events.js:96:13)
db_1 | at Socket.emit (events.js:188:7)
db_1 | at emitErrorNT (net.js:1281:8)
db_1 | at _combinedTickCallback (internal/process/next_tick.js:80:11)
db_1 | at process._tickCallback (internal/process/next_tick.js:104:9)
I have tried to go in docker machine with docker run -it mongo:3.2 /bin/bash
root#67062897d4f0:/# mongo
MongoDB shell version: 3.2.12
connecting to: test
2017-03-23T07:01:07.587+0000 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: errno:111 Connection refused
2017-03-23T07:01:07.588+0000 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect#src/mongo/shell/mongo.js:229:14
root#67062897d4f0:/# mongod
2017-03-23T07:01:12.025+0000 I CONTROL [initandlisten] MongoDB starting : pid=29 port=27017 dbpath=/data/db 64-bit host=67062897d4f0
2017-03-23T07:01:12.025+0000 I CONTROL [initandlisten] db version v3.2.12
2017-03-23T07:01:12.025+0000 I CONTROL [initandlisten] git version: ef3e1bc78e997f0d9f22f45aeb1d8e3b6ac14a14
2017-03-23T07:01:12.025+0000 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1t 3 May 2016
2017-03-23T07:01:12.025+0000 I CONTROL [initandlisten] allocator: tcmalloc
2017-03-23T07:01:12.025+0000 I CONTROL [initandlisten] modules: none
2017-03-23T07:01:12.025+0000 I CONTROL [initandlisten] build environment:
2017-03-23T07:01:12.025+0000 I CONTROL [initandlisten] distmod: debian81
2017-03-23T07:01:12.025+0000 I CONTROL [initandlisten] distarch: x86_64
2017-03-23T07:01:12.025+0000 I CONTROL [initandlisten] target_arch: x86_64
2017-03-23T07:01:12.025+0000 I CONTROL [initandlisten] options: {}
2017-03-23T07:01:12.029+0000 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=8G,session_max=20000,eviction=(threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2017-03-23T07:01:12.222+0000 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2017-03-23T07:01:12.222+0000 I CONTROL [initandlisten]
2017-03-23T07:01:12.223+0000 I CONTROL [initandlisten]
2017-03-23T07:01:12.223+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2017-03-23T07:01:12.223+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2017-03-23T07:01:12.223+0000 I CONTROL [initandlisten]
2017-03-23T07:01:12.228+0000 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2017-03-23T07:01:12.228+0000 I NETWORK [HostnameCanonicalizationWorker] Starting hostname canonicalization worker
2017-03-23T07:01:12.283+0000 I NETWORK [initandlisten] waiting for connections on port 27017
^C2017-03-23T07:01:13.447+0000 I CONTROL [signalProcessingThread] got signal 2 (Interrupt), will terminate after current cmd ends
2017-03-23T07:01:13.448+0000 I FTDC [signalProcessingThread] Shutting down full-time diagnostic data capture
2017-03-23T07:01:13.453+0000 I CONTROL [signalProcessingThread] now exiting
2017-03-23T07:01:13.453+0000 I NETWORK [signalProcessingThread] shutdown: going to close listening sockets...
2017-03-23T07:01:13.453+0000 I NETWORK [signalProcessingThread] closing listening socket: 5
2017-03-23T07:01:13.453+0000 I NETWORK [signalProcessingThread] closing listening socket: 6
2017-03-23T07:01:13.453+0000 I NETWORK [signalProcessingThread] removing socket file: /tmp/mongodb-27017.sock
2017-03-23T07:01:13.453+0000 I NETWORK [signalProcessingThread] shutdown: going to flush diaglog...
2017-03-23T07:01:13.453+0000 I NETWORK [signalProcessingThread] shutdown: going to close sockets...
2017-03-23T07:01:13.453+0000 I STORAGE [signalProcessingThread] WiredTigerKVEngine shutting down
2017-03-23T07:01:13.586+0000 I STORAGE [signalProcessingThread] shutdown: removing fs lock...
2017-03-23T07:01:13.586+0000 I CONTROL [signalProcessingThread] dbexit: rc: 0
Can someone please help me solve this ?
Amazing, I'm so glad I found this thread. I am going to add some keywords to it for other people.
If you get this error message, you could be running MongoDB on your local machine while trying to access it from inside a Docker container. The reason is that inside the Docker container, localhost or 127.0.0.1 refers to a different interface than your true local machine.
Error Output:
MongoError: failed to connect to server [127.0.0.1:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
at Pool.<anonymous> (/app/node_modules/mongodb-core/lib/topologies/server.js:329:35)
at emitOne (events.js:96:13)
at Pool.emit (events.js:191:7)
at Connection.<anonymous> (/app/node_modules/mongodb-core/lib/connection/pool.js:280:12)
at Object.onceWrapper (events.js:293:19)
at emitTwo (events.js:106:13)
at Connection.emit (events.js:194:7)
at Socket.<anonymous> (/app/node_modules/mongodb-core/lib/connection/connection.js:187:49)
at Object.onceWrapper (events.js:293:19)
at emitOne (events.js:96:13)
at Socket.emit (events.js:191:7)
at emitErrorNT (net.js:1284:8)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickDomainCallback (internal/process/next_tick.js:128:9)
name: 'MongoError',
message: 'failed to connect to server [127.0.0.1:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]' }
Possible Fixes:
Enable remote connections from your MongoDB, and use your public IP to access it from inside the Docker Container.
Run MongoDB from inside the Container, and then you can refer to it with localhost.
Run another Docker Container, and enable remote connections on it. Investigate bind_ip in the MongoDB config file. Make sure you secure your database with auth credentials. Spend some time ensuring it is secure.
Remote connections are disabled by default in MongoDB, ie: going through a Docker container incorrectly.
Here is a helpful resource: https://hub.docker.com/_/mongo/
The issue I believe is that using CMD node app.js only starts node and not the database. Normally when creating this kind of simple app you use two containers. One for mongo one for node, as you aren't using a custom config for mongo or node you could just use images and map the code in your current folder to the /opt/ directory in the container, meaning all you need is a compose file and potentially don't need any Dockerfile's
Disclaimer obviously this isn't good enough for production, at some point you will probably want to use a Dockerfile for your app as it's best to start node as a user instead of root and put the files in a meaningful directory. But if all you want to do is use as a sandbox this should do.
Your docker compose would look like
version: "2"
services:
db:
image: mongo:3.2
ports:
- 27017
app:
image: node
links:
- db
volumes:
- '.:/opt/'
command: node /opt/app.js
ports:
- 3000:3000
If you want to go full in and specify a Dockerfile for each.
/docker-files/app/Dockerfile
# take from the latest node build
FROM node
# Make a directory /opt/app
RUN mkdir /opt/app
# Set work dir to /opt/app
WORKDIR /opt/app
# Do all your npm install etc....
CMD node app.js
/docker-files/db/Dockerfile
FROM mongo:3.2
# Do some fancy mongo stuff.
/docker-compose.yml
version: "2"
services:
db:
build:
context: docker-files/db
dockerfile: Dockerfile
ports:
- 27017
app:
build:
context: docker-files/app
dockerfile: Dockerfile
links:
- db
volumes:
- '.:/opt/app'
ports:
- 3000:3000
For docker-compose, you need to use a tag networks to link one container node with other. Look:
version: '2'
networks:
app-tier:
driver: bridge
services:
mongodb:
image: 'mongodb:latest'
networks:
- app-net
myapp:
image: 'YOUR_APPLICATION_IMAGE'
networks:
- app-net

Resources