简体   繁体   中英

The mongodb in Docker container is running ,but refuse to connect?

The error message is below

MongoDB shell version: 3.2.11
connecting to: test
2020-05-16T20:53:47.438+0000 W NETWORK  [thread1] Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2020-05-16T20:53:47.440+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
@(connect):1:6

By the way, is there any way to automatically seed the database in the Docker container? I have to manually seed the data base every time.

Thank you, guys.

did you map the port of your localhost to MongoDB container? if not add -p 27017:27017 to your docker run command.

If you are looking to seed the database upon initialization, you can use the following as stated on docker hub :

When a container is started for the first time it will execute files with extensions.sh and.js that are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order. .js files will be executed by mongo using the database specified by the MONGO_INITDB_DATABASE variable, if it is present, or test otherwise. You may also switch databases within the.js script.

You can do this by mounting a javascript or shell script file via -v "./path/to/file.js:/docker-entrypoint-initdb.d/file.js" or via the volumes: ["./path/to/file.js:/docker-entrypoint-initdb.d/file.js"] key of your mongodb service if you are using docker-compose .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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