0

I have an instance of mongoDB when I execute docker-compose up -d the mongoDB container is run and he attempt to take the authentification from user.

But I use a docker-compose and I set the credentials inside of this, but when running docker-compose up the user are not connected.

Below, you can see my docker-compose.yml :

version: "2"
services:
  web:
    image: kolacaine/docker-hello-world
    ports:
    - "80:8081"
    depends_on:
    - mongo-service
  mongo-service:
    image: mongo
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: myUserAdmin
      MONGO_INITDB_ROOT_PASSWORD: abc123
    command: mongod --auth
    ports:
    - "27017:27017"

And nothing happens, he always ask authentification inside of my container.

2
  • "he always ask authentification inside of my container." "he" is referred to the web container? Commented Dec 3, 2018 at 10:49
  • Nope, for mongo-service when I running the shell of mongodb he always ask me to connect, but I set the credentials into my docker-compose Commented Dec 3, 2018 at 10:52

1 Answer 1

3

I think you misunderstood the purpose of MONGO_INITDB_ROOT_USERNAME and password, they are used to create a default root user not to provide pre-filled credential. Thus to connect to the mongo shell you need to use mongo -u myUserAdmin -p abc123.

PS command: mongod --auth is redundant, the entrypoint script does it automatically when it sees you using these 2 envs.

Sign up to request clarification or add additional context in comments.

6 Comments

Ok alright, but when I do not have the 2 envs the API works correctly, but If detect he have the 2 envs the API don't works. Why ?
@KolaCaine: because you don't specify any auth information in the client
Because now it needs a password to connect to mongo
When I use HTTP request or something else for calling my API, I always need to precise the username and password ?
@KolaCaine yes that's the point of setting a password, a connection string like mongodb://usr:pwd@mongodb:27017/testdb
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.