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"
laoqiren
  • 3,457
  • 5
  • 19
  • 29
  • Why would you expose MongoDB's port the first place? – Markus W Mahlberg Mar 30 '17 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 – Matt Mar 31 '17 at 00:07

1 Answers1

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.

Community
  • 1
  • 1
Matt
  • 68,711
  • 7
  • 155
  • 158
  • 1
    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? – laoqiren Mar 31 '17 at 06:35
  • 1
    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. – Matt Mar 31 '17 at 10:51
  • 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. – laoqiren Apr 01 '17 at 02:48
  • I'm not sure which document you are referring to, but I believe [this is the code in the entrypoint](https://github.com/docker-library/mongo/blob/812b8fb401a929c312c7222a26b707a6415450c6/3.4/docker-entrypoint.sh#L4-L6) that is causing the confusion. – Matt Apr 01 '17 at 07:01