Mongo: cannot docker run -v external database

Created on 26 Apr 2017  Â·  4Comments  Â·  Source: docker-library/mongo

My docker version is 17.03.1-ce and I'm using mongo:3.4 as my base image. I want the mongo db in container to use an external database which is at /data/db (relative path)

I tried docker run -d -v /data/db:/data/db -p 28000:27017 mongo:3.4 but it encountered an error:

chown: changing ownership of '/data/db/collection-0--2543004606942673213.wt': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2015-01-01T00-04-35Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2016-01-01T00-00-31Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2016-01-01T00-00-44Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2016-11-10T02-59-43Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2016-11-17T01-09-33Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2016-11-17T02-49-26Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2016-11-18T23-22-58Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2016-11-20T19-05-32Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2016-11-20T19-13-05Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2016-11-24T00-02-06Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2016-11-29T00-43-43Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2016-11-30T23-32-16Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2016-12-03T16-46-13Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2016-12-06T00-05-48Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2016-12-15T04-11-38Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2016-12-22T05-04-19Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2016-12-22T18-42-46Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2016-12-27T03-32-48Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2016-12-27T18-51-09Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2017-01-14T01-28-13Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2017-01-17T00-45-29Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2017-01-22T05-49-02Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2017-01-31T23-57-02Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2017-02-01T07-06-19Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2017-02-20T01-56-20Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2017-03-01T22-58-28Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2017-03-06T02-32-03Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2017-03-07T00-37-32Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2017-03-08T01-11-42Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2017-03-11T17-47-02Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2017-03-16T20-04-52Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2017-03-17T02-26-58Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2017-03-22T15-13-51Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2017-03-22T17-54-27Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2017-03-25T03-43-06Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2017-03-26T23-07-40Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2017-03-31T22-32-40Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2017-04-02T17-38-14Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2017-04-03T16-58-08Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2017-04-06T18-03-38Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2017-04-13T19-25-56Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2017-04-14T14-11-36Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2017-04-14T17-26-26Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2017-04-15T22-59-39Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2017-04-18T16-50-30Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2017-04-19T04-38-47Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2017-04-21T19-25-14Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2017-04-24T01-43-13Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.2017-04-25T18-06-21Z-00000': Operation not permitted
chown: changing ownership of '/data/db/diagnostic.data/metrics.interim': Operation not permitted
chown: changing ownership of '/data/db/index-1--2543004606942673213.wt': Operation not permitted
chown: changing ownership of '/data/db/journal/WiredTigerLog.0000000051': Operation not permitted
chown: changing ownership of '/data/db/journal/WiredTigerPreplog.0000000001': Operation not permitted
chown: changing ownership of '/data/db/journal/WiredTigerPreplog.0000000002': Operation not permitted
chown: changing ownership of '/data/db/WiredTiger.turtle': Operation not permitted
chown: changing ownership of '/data/db/WiredTigerLAS.wt': Operation not permitted

How do I solve this issue? Thanks

Most helpful comment

The container will try to chown the data directory it is given, see entrypoint, but it looks like it is not allowed to on your system.

The two solutions are:

  • chown the directory and contents from your host sudo chown -R 999:999 /data/db

    • 999 is the id of the mongodb user in the image

    • this will only work if you are not using user namespaces

  • just run the container with the current owner of the files
    docker run -d -v /data/db:/data/db -p 28000:27017 --user id-of-file-owner mongo:3.4

    • ls -ln /data/db/ on the host to see the current owner

    • this is probably the better solution since you can easily switch back to non-container if desired

All 4 comments

The container will try to chown the data directory it is given, see entrypoint, but it looks like it is not allowed to on your system.

The two solutions are:

  • chown the directory and contents from your host sudo chown -R 999:999 /data/db

    • 999 is the id of the mongodb user in the image

    • this will only work if you are not using user namespaces

  • just run the container with the current owner of the files
    docker run -d -v /data/db:/data/db -p 28000:27017 --user id-of-file-owner mongo:3.4

    • ls -ln /data/db/ on the host to see the current owner

    • this is probably the better solution since you can easily switch back to non-container if desired

Should passing --user id-of-file-owner work in the case that we are using user namespaces? That is, if my docker/daemon.json says "userns-remap": "someusr:somegrp"?

I am getting exception in initAndListen: IllegalOperation: Attempted to create a lock file on a read-only directory: /data/db, terminating when I pass--user 4003:1001 (e.g. userid:grpid).

What is are the required permissions on the host directory?

Currently I have drwxrwsr-x

If you want to use user namespaces in conjunction with bind mounts, you're very likely to have a bad time. In order to get that working, you'll need to chown the directory to the _remapped_ UID/GID of the container you're trying to run, so whatever your userns-remap base ID is _plus_ the UID of the user you're trying to run as.

For example, if your /etc/subuid entry specifies 165536:65536, you'll probably need to do 165536 plus 4003 to determine the appropriate UID for your volume (169539).

Thank I did actually get this to work, and you’re right it’s not simple.

I had my the following in /etc/subuid
usrblah 500000

And in /etc/subgid
grpblah 500000

I run the daemon with remapping usrblah:grpblah

I observed that docker creates its own folder with ownership 500000:500000
So I mirrored this on the dir I wanted to bind and it worked. It was not
necessary to ‘add’ the value if $(id -u userblah)

Thanks.

On July 30, 2018 at 1:58:01 PM, Tianon Gravi ([email protected])
wrote:

If you want to use user namespaces in conjunction with bind mounts, you're
very likely to have a bad time. In order to get that working, you'll need
to chown the directory to the remapped UID/GID of the container you're
trying to run, so whatever your userns-remap base ID is plus the UID of
the user you're trying to run as.

For example, if your /etc/subuid entry specifies 165536:65536, you'll
probably need to do 165536 plus 4003 to determine the appropriate UID for
your volume (169539).

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/docker-library/mongo/issues/173#issuecomment-409007942,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AF-GlO9wqes-qKo8dKXa90BXrcaw-d2Cks5uL3NYgaJpZM4NIP9q
.

Was this page helpful?
0 / 5 - 0 ratings