Error in MongoDB with docker compose when try to use a command in the YAML file - mongodb

version: '3.1'
services:
mongo:
image: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
MONGO_INITDB_DATABASE: test
volumes:
- /home/victor/Desarrollo/docker/mongo:/data/db
- ./init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js:ro
ports:
- "27017:27017"
command:
- init-mongo.sh
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: example
and this init-mongo.sh in the same directory
db.auth("root","example")
use test
db.createUser(
{
user: "myTester",
pwd: "example", // or cleartext password
roles: [ { role: "readWrite", db: "test" } ]
}
)
db.createCollection("collectiontest");
when I build up this, the services wont start, it says its restarting, if I quit the command option from YAML, it works, but I need to create a user and a collection when I install MongoDB with docker.
How can I do that?
Thanks.

Related

Mongo image does not created with credentials

I am creating mongo:latest container on my machine using docker-compose.
This is my docker-compose.yaml:
version: '3.6'
services:
backend:
image: backend:1.0.0
container_name: backend
ports:
- '5000:5000'
links:
- mongodb
environment:
NODE_ENV: PRODUCTION
API_PORT: 5000
MONGO_USERNAME: root
MONGO_PASSWORD: rootPassXXX
MONGO_HOST: mongodb
MONGO_PORT: 27017
MONGO_DATABASE: automatic
JWT_TOKEN: Auto
scheduler:
image: scheduler:1.0.0
container_name: scheduler
links:
- mongodb
environment:
NODE_ENV: PRODUCTION
API_PORT: 5000
MONGO_USERNAME: root
MONGO_PASSWORD: rootPassXXX
MONGO_HOST: mongodb
MONGO_PORT: 27017
MONGO_DATABASE: automatic
mongodb:
image: mongo
container_name: mongodb
expose:
- 27017
ports:
- '27017:27017'
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=rootPassXXX
volumes:
- ./conf/mongodb:/data/db
volumes:
autoc:
external: true
networks:
default:
external:
name: automatic
After creating those containers I am receiving from them an error: MongoServerError: Authentication failed.
This is the URI that the application is trying to connect: mongodb://root:rootPassXXX#mongodb:27017/matiautomai.
While trying to investigate what was happening, I was trying to log in to the same mongo container
that initializes with the user name and password variables without the credentials using that way: mongodb://mongodb:27017/automatic, And the application manages to connect to it.
I've reviewed the docs inside the Docker hub and it seems to be that I used the environment variables in the correct way.
What am I missing here?
After seeing your helpful comments, I found the solution for this issue.
I needed to initialize a one-time docker-compose with an initial script that located inside.
docker-compose.yaml:
version: '3.7'
services:
mongodb:
container_name: mongodb
image: mongo:latest
restart: always
ports:
- 27017:27017
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: password
MONGO_INITDB_DATABASE: root-db
volumes:
- ./docker-entrypoint-initdb.d/mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
This is the script that located inside ./docker-entrypoint-initdb.d/mongo-init.js:
print('start ~~~~');
db = db.getSiblingDB('yourDBname');
db.createUser(
{
user: 'username',
pwd: 'password',
roles: [{ role: 'readWrite', db: 'yourDBname' }],
},
);
db.createCollection('users');
print('end ~~~~');
After that, you will be able to connect to mongo with the credential you have set in the .js file.

Cannot connect to MongoDB outside the docker

I've got the following docker compose file with MongoDB container:
version: '3.6'
services:
mongo:
image: mongo
volumes:
- ./keyfile.txt:/data/keyfile.txt
entrypoint:
- bash
- -c
- |
cp /data/keyfile.txt /data/replica.key
chmod 400 /data/replica.key
chown 999:999 /data/replica.key
exec docker-entrypoint.sh $$#
command: "--bind_ip_all --replSet rs0 --keyFile /data/replica.key"
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
healthcheck:
test: test $$(echo "rs.initiate().ok || rs.status().ok" | mongo -u root -p example --quiet) -eq 1
interval: 10s
retries: 3
network_mode: host
I have to get rid of network_mode: host, so I changed it to
ports:
- "27017:27017"
After that change, I'm not able to connect to the mongo outside of the docker network.
I'm trying to connect to the database using Compass with the following URI:
mongodb://root:example#localhost:27017/?authMechanism=DEFAULT
How to fix that?
This is fully working example.
I'm recommending to create custom DB and custom user instead of using root user.
Use special path of auto-executing queries and create one or multiple databases.
mongodb:
image: mongo:latest
restart: always
ports:
- "27017:27017"
volumes:
- ./var/mongodb:/data/db
- ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: pass
mongo-init.js
db = db.getSiblingDB('SOME_DB');
db.createUser({
user: 'some_db_user',
pwd: '12345',
roles: [{ role: 'readWrite', db: 'SOME_DB' }],
});
db = db.getSiblingDB('SOME_OTHER_DB');
db.createUser({
user: 'some_other_db_user',
pwd: '12345',
roles: [{ role: 'readWrite', db: 'SOME_OTHER_DB' }],
});
db.createCollection('test');

Connecting using MongoDB Compass to docker mongo image

I'm trying to see the contents of a collection using MongoDB Compass. I have username/password authentication set up. I can log in successfully but can't see any documents in the collection. Instead, I see the error:
An error occurred while loading navigation: command hostInfo requires authentication.
Docker Compose file
version: '3.7'
services:
mongo:
image: mongo
restart: always
ports:
- 27017:27017
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: pass
volumes:
- ./mongo/init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js:ro
- ./mongo:/data/db
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_BASICAUTH_USERNAME: rmurad93
ME_CONFIG_BASICAUTH_PASSWORD: Qwert111
ME_CONFIG_MONGODB_PORT : 27017
ME_CONFIG_MONGODB_ADMINUSERNAME: admin
ME_CONFIG_MONGODB_ADMINPASSWORD: pass
init-mongo.js file
use admin
db.createUser({
user: "rmurad93",
pwd: "password",
roles: [ { role: "root", db: "admin" } ]
})
Line in terminal mongo compass
mongodb://localhost:27017/rmurad93:password
to connect to MongoCompass you should follow this pattern
mongodb://USERNAME:PASSWORD#localhost:27017/DB_NAME
If you still can't connecting, try using (Fill in connection fields individually)
option shown above instead of (Paste connection string)

Docker compose does not create volume docker-entrypoint-initdb.d for mongodb

I have the following docker-compose.yaml file, which generates a mongo and a mongo-express image:
version: '3.6'
services:
mongo:
image: mongo:4.0.4
restart: always
container_name: mongodbtest
ports:
- 27017:27017
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: 123456789
MONGO_INITDB_DATABASE: experts
volumes:
- docker-data:/data/db
- ./db-init/:/docker-entrypoint-initdb.d
mongo-express:
image: mongo-express
restart: always
container_name: mongodbexpress
depends_on:
- mongo
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: 123456789
volumes:
docker-data:
name: experts-mongoDB-data
In the folder init-db/init.js I have the following script:
db.createUser({
user: "andrea",
pwd: "123456789",
roles: [ { role: "readWrite", db: "experts" } ]
})
db.experts.insert({
name: "Steve Jobs"
})
Despite that, when I run docker-compose up the container is created but the database experts is not. Does anyone know where this problem lies? I appreciate any help you can provide

MongoDB docker-entrypoint issue

MongoDB docker-entrypoint ignoring custom script for creating user and database.
I've tried on many ways. Here is one of my configuration.
Solution 1
docker-compose.yml (version 1)
version: '3.2'
services:
erste-mongodb:
build:
context: .
dockerfile: mongodb.dockerfile
image: erste/lts:mongodb
#environment:
# MONGO_INITDB_ROOT_USERNAME: "root"
# MONGO_INITDB_ROOT_PASSWORD: "toor"
volumes:
- ./data/mongo-data:/data/db
# - ./data/mongo-init:/docker-entrypoint.d/
ports:
- "27017:27017"
networks:
- mynet
networks:
mynet:
dockerfile
FROM mongo:latest
COPY ./data/mongo-init/mongo-init.sh /docker-entrypoint.d/
Solution 2
docker-compose.yml (version 2)
version: '3.2'
services:
erste-mongodb:
build:
context: .
dockerfile: mongodb.dockerfile
image: erste/lts:mongodb
environment:
MONGO_INITDB_ROOT_USERNAME: "root"
MONGO_INITDB_ROOT_PASSWORD: "toor"
volumes:
- ./data/mongo-data:/data/db
- ./data/mongo-init:/docker-entrypoint.d/
ports:
- "27017:27017"
networks:
- mynet
networks:
mynet:
dockerfile
FROM mongo:latest
Here is my init script:
mongo-init.sh
#!/bin/bash
echo "Creating users..."
mongo --eval "db.createUser({user: 'revision', pwd: 'rev123',roles: [{role: 'dbOwner', db: 'ebmn_log'}]});"
mongo -u revision -p rev123 --eval "db = db.getSiblingDB('ebmn_log');
db.createCollection('journal')";
echo "Finishing with users"
Does anyone have an idea how to run initialization script.
Thank you in advance.
The problem was that I have used ./data/mongo-init:/docker-entrypoint.d/ instead of ./data/mongo-init:/docker-entrypoint-initdb.d/.
I have changed that and it works like charm.
P.S. Instead of sh script is always better to use pure js.