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:
Related
I'm trying to get mongo server up and running in a docker container but getting error:
2020-07-29T21:15:58.518-0500 I CONTROL [main] ***** SERVER RESTARTED *****
2020-07-29T21:15:58.621-0500 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2020-07-29T21:15:58.681-0500 W ASIO [main] No TransportLayer configured during NetworkInterface startup
2020-07-29T21:15:58.684-0500 I CONTROL [initandlisten] MongoDB starting : pid=747 port=27017 dbpath=/app/database 64-bit host=610312a6720b
2020-07-29T21:15:58.686-0500 I CONTROL [initandlisten] db version v4.2.8
2020-07-29T21:15:58.687-0500 I CONTROL [initandlisten] git version: 43d25964249164d76d5e04dd6cf38f6111e21f5f
2020-07-29T21:15:58.689-0500 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.1.1 11 Sep 2018
2020-07-29T21:15:58.690-0500 I CONTROL [initandlisten] allocator: tcmalloc
2020-07-29T21:15:58.692-0500 I CONTROL [initandlisten] modules: none
2020-07-29T21:15:58.693-0500 I CONTROL [initandlisten] build environment:
2020-07-29T21:15:58.694-0500 I CONTROL [initandlisten] distmod: ubuntu1804
2020-07-29T21:15:58.696-0500 I CONTROL [initandlisten] distarch: x86_64
2020-07-29T21:15:58.697-0500 I CONTROL [initandlisten] target_arch: x86_64
2020-07-29T21:15:58.698-0500 I CONTROL [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "0.0.0.0", port: 27017 }, processManagement: { timeZoneInfo: "/usr/share/zoneinfo" }, storage: { dbPath: "/app/database", journal: { enabled: true } }, systemLog: { destination: "file", logAppend: true, path: "/app/database/log/mongodb/mongod.log" } }
2020-07-29T21:15:58.711-0500 I STORAGE [initandlisten] Detected data files in /app/database created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2020-07-29T21:15:58.715-0500 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=483M,cache_overflow=(file_max=0M),session_max=33000,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,close_scan_interval=10,close_handle_minimum=250),statistics_log=(wait=0),verbose=[recovery_progress,checkpoint_progress],
2020-07-29T21:15:59.442-0500 E STORAGE [initandlisten] WiredTiger error (1) [1596075359:442302][747:0x7faa34ed4b00], file:WiredTiger.wt, connection: __posix_open_file, 667: /app/database/WiredTiger.wt: handle-open: open: Operation not permitted Raw: [1596075359:442302][747:0x7faa34ed4b00], file:WiredTiger.wt, connection: __posix_open_file, 667: /app/database/WiredTiger.wt: handle-open: open: Operation not permitted
2020-07-29T21:15:59.475-0500 E STORAGE [initandlisten] WiredTiger error (1) [1596075359:475036][747:0x7faa34ed4b00], file:WiredTiger.wt, connection: __posix_open_file, 667: /app/database/WiredTiger.wt: handle-open: open: Operation not permitted Raw: [1596075359:475036][747:0x7faa34ed4b00], file:WiredTiger.wt, connection: __posix_open_file, 667: /app/database/WiredTiger.wt: handle-open: open: Operation not permitted
2020-07-29T21:15:59.504-0500 E STORAGE [initandlisten] WiredTiger error (1) [1596075359:504949][747:0x7faa34ed4b00], file:WiredTiger.wt, connection: __posix_open_file, 667: /app/database/WiredTiger.wt: handle-open: open: Operation not permitted Raw: [1596075359:504949][747:0x7faa34ed4b00], file:WiredTiger.wt, connection: __posix_open_file, 667: /app/database/WiredTiger.wt: handle-open: open: Operation not permitted
2020-07-29T21:15:59.533-0500 E STORAGE [initandlisten] WiredTiger error (1) [1596075359:533624][747:0x7faa34ed4b00], file:WiredTiger.wt, connection: __posix_open_file, 667: /app/database/WiredTiger.wt: handle-open: open: Operation not permitted Raw: [1596075359:533624][747:0x7faa34ed4b00], file:WiredTiger.wt, connection: __posix_open_file, 667: /app/database/WiredTiger.wt: handle-open: open: Operation not permitted
2020-07-29T21:15:59.538-0500 W STORAGE [initandlisten] Failed to start up WiredTiger under any compatibility version.
2020-07-29T21:15:59.539-0500 F STORAGE [initandlisten] Reason: 1: Operation not permitted
2020-07-29T21:15:59.540-0500 F - [initandlisten] Fatal Assertion 28595 at src/mongo/db/storage/wiredtiger/wiredtiger_kv_engine.cpp 915
2020-07-29T21:15:59.540-0500 F - [initandlisten]
Running the following command starts the server fine and is accessible outside docker container:
mongod --dbpath /app/database
but when following command is ran Mongo logs the above mentioned error log into the log file
service mongodb start
or
sudo service mongodb start
Not sure why this issue is causing. Kindly help. Thank you.
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
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.
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
I've been trying to set up MongoDB on my Ubuntu 16.04.2 LTS (GNU/Linux 4.4.0-22-generic x86_64) VPS and I've been running into a few problems. I am running as a user on my server that has root access but not using the root account itself.
I started to notice the problem when I was executing db.createUser() on the admin database so I could add other users and I was getting this error:
not authorized to execute command
So I then proceeded to uninstall and install again using the tutorial as provided on the website: https://docs.mongodb.com/v3.2/tutorial/install-mongodb-on-ubuntu/
Now when I'm on stage 2 Verify that MongoDB has started successfully on the Run MongoDB Community Edition section, that is I look at my output from the file /var/log/mongodb/mongod.log, I get the following:
2017-04-14T14:23:09.309+0200 I CONTROL [main] ***** SERVER RESTARTED *****
2017-04-14T14:23:09.316+0200 I CONTROL [initandlisten] MongoDB starting : pid=9691 port=27017 dbpath=/var/lib/mongodb 64-bit host=vps338741
2017-04-14T14:23:09.316+0200 I CONTROL [initandlisten] db version v3.4.3
2017-04-14T14:23:09.316+0200 I CONTROL [initandlisten] git version: f07437fb5a6cca07c10bafa78365456eb1d6d5e1
2017-04-14T14:23:09.316+0200 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2g 1 Mar 2016
2017-04-14T14:23:09.316+0200 I CONTROL [initandlisten] allocator: tcmalloc
2017-04-14T14:23:09.316+0200 I CONTROL [initandlisten] modules: none
2017-04-14T14:23:09.316+0200 I CONTROL [initandlisten] build environment:
2017-04-14T14:23:09.316+0200 I CONTROL [initandlisten] distmod: ubuntu1604
2017-04-14T14:23:09.316+0200 I CONTROL [initandlisten] distarch: x86_64
2017-04-14T14:23:09.316+0200 I CONTROL [initandlisten] target_arch: x86_64
2017-04-14T14:23:09.316+0200 I CONTROL [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "127.0.0.1", port: 27017 }, storage: { dbPath: "/var/lib/mongodb", journal: { enabled: true } }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log", quiet: true } }
2017-04-14T14:23:09.349+0200 I STORAGE [initandlisten]
2017-04-14T14:23:09.349+0200 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2017-04-14T14:23:09.349+0200 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2017-04-14T14:23:09.349+0200 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=464M,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),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2017-04-14T14:23:09.366+0200 E STORAGE [initandlisten] WiredTiger error (2) [1492172589:366629][9691:0x7f1755946cc0], file:WiredTiger.wt, connection: /var/lib/mongodb/WiredTiger.wt: handle-open: open: No such file or directory
2017-04-14T14:23:09.367+0200 I - [initandlisten] Assertion: 28595:2: No such file or directory src/mongo/db/storage/wiredtiger/wiredtiger_kv_engine.cpp 267
2017-04-14T14:23:09.367+0200 I STORAGE [initandlisten] exception in initAndListen: 28595 2: No such file or directory, terminating
2017-04-14T14:23:09.367+0200 I NETWORK [initandlisten] shutdown: going to close listening sockets...
2017-04-14T14:23:09.367+0200 I NETWORK [initandlisten] removing socket file: /tmp/mongodb-27017.sock
2017-04-14T14:23:09.367+0200 I NETWORK [initandlisten] shutdown: going to flush diaglog...
2017-04-14T14:23:09.367+0200 I CONTROL [initandlisten] now exiting
2017-04-14T14:23:09.367+0200 I CONTROL [initandlisten] shutting down with code:100
I've looked around and it is not the same error as found here, because that is to do with the fact there isn't an /data/db folder which I have. From that log I can see it is something to do with this line:
2017-04-14T14:23:09.366+0200 E STORAGE [initandlisten] WiredTiger error (2) [1492172589:366629][9691:0x7f1755946cc0], file:WiredTiger.wt, connection: /var/lib/mongodb/WiredTiger.wt: handle-open: open: No such file or directory
Which is true, when I go to /var/lib/mongodb and do ls -l it results with:
drwxr-xr-x 2 mongodb mongodb 4096 Apr 14 14:10 journal
-rw-r--r-- 1 mongodb mongodb 0 Apr 14 14:02 mongod.lock
-rw-r--r-- 1 mongodb mongodb 0 Apr 14 14:10 WiredTiger
-rw-r--r-- 1 mongodb mongodb 21 Apr 14 14:10 WiredTiger.lock
-rw-r--r-- 1 mongodb mongodb 1004 Apr 14 14:10 WiredTiger.turtle
But I don't know if I should manually create that file as not seen this problem elsewhere. I made sure that mongodb user has permissions on these files too as stated in the answer to this question, but still no luck.
I'm really at a loss as to what to do, any help would be very appreciated as this is my like fifth attempt at getting it working!
Thanks!
If you created the mkdir -p /data/db maybe there is a chance that you must start your server with the following command : sudo mongod
macOS/OSX : https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/
try :
sudo service mongod start ;
then run the mongodb with:
mongo
This is a known bug in MongoDB, check the report
Your problem is not that files are missing in general, it's that they go missing when you restart after a clean shutdown. Your problem is not related to the /data/db directory but more to WiredTiger.wt file that is missing after a restart.
Oct 1 20:00:44 alpha mongod.27017[1074]: [initandlisten] WiredTiger
(2) [1443729644:306783][1074:0x7fdba8fd4bc0], file:WiredTiger.wt,
connection: /data/db/WiredTiger.wt: No such file or directory
The bug report talks about the same thing.
Try:
I would suggest try and disable WiredTiger in the config file.
MongoDB Config Options
WiredTiger
UPDATE: To disable WiredTiger you have to change the storage.engine option in your mongo config file, by default mongod uses WiredTiger but you can also use InMemory LINK
Make a directory
sudo mkdir /data/db
Provide access to the directory
sudo chown -R $USER /data/db
Then run
mongod
If you get port already in use the stop mongod, it might be running in the background
sudo service mongod stop
If the last line is
waiting for connections on port 27017
then the mongod is working
This command helped me:
mongod --dbpath /data/db --repair
for me the 'answer' was to execute the following command (since service mongod start and just mongod failed)
sudo mongod --fork --port 27017 --replSet admin --logpath /data/log/mongo.log
I did not need to turn off wired tiger
Docker container:
In my case, I was using a docker container running
mongodb:
image: 'bitnami/mongodb:latest'
user: root
command: "chown -R $USER /bitnami/mongodb"
volumes:
- '/var/www/mongodb_data:/bitnami'
environment:
- MONGODB_USERNAME=bn_parse
- MONGODB_PASSWORD=3yfQexfqcc
- MONGODB_DATABASE=bitnami_parse
I migrated the docker host to other VPS/VM (aws is too expensive).
I added temporarily the command "chown -R $USER /bitnami/mongodb" to change the owner of that folder /bitnami/mongodb. It worked.
sudo mdkir data/db
sudo service mongod stop
sudo mongod
mongosh
this fixed it for me on ubuntu 22.04 after hours and hours of hunting down solutions