Reputation: 5813
This docker-compose.yml
:
services:
database:
image: mongo:3.2
ports:
- "27017"
command: "mongod --dbpath=/usr/database"
networks:
- backend
volumes:
- dbdata:/usr/database
volumes:
dbdata:
results in this error (snipped):
database_1 | 2016-11-28T06:30:29.864+0000 I STORAGE [initandlisten] exception in initAndListen: 98 Unable to create/open lock file: /usr/database/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating
Ditto for just trying to run the command in a container using that image directly:
$ docker run -v /usr/database mongo:3.2 mongod --dbpath=/usr/database
But, if I run /bin/bash
when starting the container, and THEN start mongo
, we're OK:
$ docker run -it -v /usr/database mongo:3.2 /bin/bash
root@8aab722fad89:/# mongod --dbpath=/usr/database
Based on the output, the difference seems to be that in the second scenario, the command is run as root.
So, my questions are:
/bin/bash
method work, when the others do not?Note: On OSX, since that seems to effect whether you can mount a host directory as a volume for Mongo to use - not that I'm doing that.
Upvotes: 2
Views: 6728
Reputation: 18835
To clarify, this image hub.docker.com/_/mongo is an official MongoDB docker image from DockerHub, but NOT an official docker image from MongoDB.
Now to answer your questions,
Why does the
/bin/bash
method work, when the others do not?
This answer is based on Dockerfile v3.2. First to point out that your volume mount command -v /usr/database
, is essentially creating a directory in the container with the root ownership permission.
Your command below failed with permission denied
because the the docker image is running the command as user mongodb
(see this dockerfile line) . As the directory /usr/database
is owned by root
.
$ docker run -v /usr/database mongo:3.2 mongod --dbpath=/usr/database
While if you execute below /bin/bash
then manually run mongod
:
$ docker run -it -v /usr/database mongo:3.2 /bin/bash
Your are logged in as root
and executing mongod
as root
, and it has the permission to create database files in /usr/database/
.
Also, if you're executing the line below, it works because you're pointing to a directory /data/db
which the permission has been corrected for user mongodb
(see this dockerfile line)
$ docker run -v db:/data/db mongo:3.2
How can I replicate that reason, in the docker-compose?
The easiest solution is to use command: "mongod --dbpath=/data/db"
because the permission ownership has been corrected in the Dockerfile.
If you are intending to use a host volume, you probably would have to add mongodb
user on your host OSX and change appropriate directories permission. Modifying the permission ownership of a volume mount is outside the scope of docker-compose.
Upvotes: 2