Afnan Ahmad
Afnan Ahmad

Reputation: 2542

Mongodb database could not be created on docker container startup

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.

Upvotes: 2

Views: 730

Answers (1)

Neo Anderson
Neo Anderson

Reputation: 6350

The problem seems to be in your docker run command.
You are building the image image1:1.0 but you are running the container using the mongo image.
The --name defines the container name. The last argument from the docker run command refers to the image to be used.

Run the container:

docker run -d -p 27017-27019:27017-27019 --name my-awesome-container image1:1.0

List the containers: you need the containerID to open a console in the next step:

docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                  NAMES
b48ba39b0d38        image1:1.0          "docker-entrypoint.s…"   15 minutes ago      Up 15 minutes       0.0.0.0:27017-27019->27017-27019/tcp   my-awesome-container

Connect to my-awesome-container:

docker exec -ti b48ba39b0d38 sh

Connect to the DB using the following command: (use admin-password when asked for the passowrd)

mongo --username admin-user
MongoDB shell version v4.2.8
Enter password: 
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("17b28c1e-edb7-4c41-93bd-bdc3abcb47a6") }
MongoDB server version: 4.2.8
Server has startup warnings: 
2020-07-30T12:20:09.577+0000 I  STORAGE  [initandlisten] 
2020-07-30T12:20:09.577+0000 I  STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2020-07-30T12:20:09.577+0000 I  STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

> 

List the databases:

> show databases
admin          0.000GB
config         0.000GB
local          0.000GB
test-database  0.000GB

Enjoy using the test-database that you created with the mongo-init.js

Upvotes: 1

Related Questions