how to add --auth for mongodb image when using docker-compose?

14,079

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.

Share:
14,079

Related videos on Youtube

laoqiren
Author by

laoqiren

I'm a web developer now working in Bytedance Inc. --- July, 2019

Updated on June 04, 2022

Comments

  • laoqiren
    laoqiren almost 2 years

    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
    laoqiren about 7 years
    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?
  • Matt
    Matt about 7 years
    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.
  • laoqiren
    laoqiren about 7 years
    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.
  • Matt
    Matt about 7 years
    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.