I am trying to initialize Mongodb database with collection in it and its own new user.
I have followed the following SO link but I am unable to connect to database from host.
If I open a Mongodb Shell I am unable to find my newly created database and user.
Scenario 1
Docker File
FROM mongo:latest
ENV MONGO_INITDB_ROOT_USERNAME admin-user
ENV MONGO_INITDB_ROOT_PASSWORD admin-password
ENV MONGO_INITDB_DATABASE admin
ADD mongo-init.js /docker-entrypoint-initdb.d/
mongo-init.js
db.auth('admin-user', 'admin-password')
db = db.getSiblingDB('test-database')
db.createUser({
user: 'test-user',
pwd: 'test-password',
roles: [
{
role: 'readWrite',
db: 'test-database',
},
],
});
db.foo.insert({ x: 1, y: 1 })
I have linux ubuntu. I have used the following commmands:
docker image build -t image1:1.0 .
docker run -d -p 27017-27019:27017-27019 --name image1 mongo
For above scenario Neo Anderson answer worked perfectly. Now I have another scenario:
Scenario 2:
If I try to connect with my container Mongodb outside from the container I am unable to connect. Failed to connect to 172.20.1.100:37019. Though I have changed the run command as well to run -d -p 37017:27017-27019.