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.
Related videos on Youtube
Author by
laoqiren
I'm a web developer now working in Bytedance Inc. --- July, 2019
Updated on June 04, 2022Comments
-
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 about 7 yearsthanks, 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 about 7 yearsThe 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 about 7 yearsbut 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 about 7 yearsI'm not sure which document you are referring to, but I believe this is the code in the entrypoint that is causing the confusion.