9

I'm using docker-compose to run my project created by node,mongodb,nginx;

and I have build the project using docker build

and then I use docker up -d nginx to start my project. but I haven't found the config to run mongodb image with '--auth', so how to add '--auth' when compose start the mongodb?

here is my docker-compose.yml:

version: "2"
services:
  mongodb:
    image: mongo:latest
    expose:
        - "27017"
    volumes:
        - "/home/open/mymongo:/data/db"
  nginx:
    build: /home/open/mynginx/
    ports:
        - "8080:8080"
        - "80:80"
    links:
        - node_server:node
  node_server:
    build: /home/laoqiren/workspace/isomorphic-redux-CNode/ 
    links:
        - mongodb:mongo
    expose:
        - "3000"
2
  • Why would you expose MongoDB's port the first place? Commented Mar 30, 2017 at 20:11
  • @MarkusWMahlberg It looks like a hangover from a v1 compose file. Exposing a port without mapping it from the docker host made it available to other containers. It doesn't do much in v2 onwards where compose creates a standalone docker network that has open communications between containers on that network Commented Mar 31, 2017 at 0:07

1 Answer 1

16

Supply a command to the container including the --auth option.

  mongodb:
    image: mongo:latest
    expose:
        - "27017"
    volumes:
        - "/home/open/mymongo:/data/db"
    command: mongod --auth

The latest mongo containers come with "root" auth initialisation via environment variables too, modelled on the postgres setup.

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

4 Comments

thanks, but why is it OK to just set command be --auth? I have read the dockerfile of mongodb image, I can't understand why its CMD is ['mongod'] and its entrypoint is docker-entrypoint.sh, but I can run the image by 'docker run mongo --auth', since the --auth will replace the mongod CMD, so it will be '--auth' not 'mongod --auth', why it can run?
The image defaults to running mongod when nothing is supplied and also when arguments starting with a - are supplied. Otherwise it will run the command you provide.
but the document said if ENTRYPOINT and CMD are both provided, the value in CMD will be default parameter of ENTRYPOINT, so why the --auth will not replace the mongod but be added to CMD? --auth is also a parameter.
I'm not sure which document you are referring to, but I believe this is the code in the entrypoint that is causing the confusion.

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.