Reputation: 3262
I am able to set the username, password and create database using the docker compose.yml file
version: '3'
services:
mongodb:
image: mongo
volumes:
- /home/temp/finalewd/temp:/var/lib/mongodb
restart: on-failure
ports:
- "27017:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: XXXX
MONGO_INITDB_ROOT_PASSWORD: XXXX
MONGO_INITDB_DATABASE: XXXX
It brings up the new mongo container with database as XXXX , username and password configured.
But when I try to set up the mongo docker container with replica set like below,
# Use root/example as user/password credentials
version: '3.1'
services:
mongo:
image: mongo
restart: on-failure
volumes:
- /home/temp/mongo/compose/data:/data/db
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all","--replSet", "rs0" ]
environment:
MONGO_INITDB_ROOT_USERNAME: XXXX
MONGO_INITDB_ROOT_PASSWORD: XXXX
MONGO_INITDB_DATABASE: XXXX
Using the above docker-compose.yml, It is bringing up with replica set but not creating the database / username / password.
Why the environment variable is not used in the above case?
Anyhelp is appreciated.
Content Added for Thomas's answer :
Tried the docker-compose.yml using the content in the Thomas's answer and once the mongo is up, I tried "rs.initiate but its throwing the already initialized status so How to bring the mongo up with master status in this case?
rs0:OTHER> rs.status();
{
"operationTime" : Timestamp(1552397666, 1),
"ok" : 0,
"errmsg" : "Our replica set config is invalid or we are not a member of it",
"code" : 93,
"codeName" : "InvalidReplicaSetConfig",
"$clusterTime" : {
"clusterTime" : Timestamp(1552397666, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
rs0:OTHER> rs.initiate()
{
"operationTime" : Timestamp(1552397666, 1),
"ok" : 0,
"errmsg" : "already initialized",
"code" : 23,
"codeName" : "AlreadyInitialized",
"$clusterTime" : {
"clusterTime" : Timestamp(1552397666, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
rs0:OTHER>
Thanks, Harry
Upvotes: 6
Views: 10329
Reputation: 103915
The original entrypoint from the mongo docker image is responsible for creating the (eventually missing) database on container start.
Since you overridden this entry point in your docker-compose.yml file (entrypoint: [ "/usr/bin/mongod", "--bind_ip_all","--replSet", "rs0" ]
), you are loosing all the features from the original entrypoint.
This docker image documentation tells us we can pass options to the mongo process using the docker compose command:
directive. Your docker-compose.yml file should be:
version: '3.1'
services:
mongo:
image: mongo
restart: on-failure
volumes:
- /home/temp/mongo/compose/data:/data/db
command: "--bind_ip_all --replSet rs0"
environment:
MONGO_INITDB_ROOT_USERNAME: XXXX
MONGO_INITDB_ROOT_PASSWORD: XXXX
MONGO_INITDB_DATABASE: XXXX
Upvotes: 11