31

I would like to persist the mongoDB data outside of the container and on a specified volume. I am using docker-compose and the yml file looks like

web:
  build: .
  command: python -u app.py
  ports:
    - "5000:5000"
  volumes:
    - .:/todo
  links:
    - db
db:
  image: mongo:3.0.2
Ian
  • 30,182
  • 19
  • 69
  • 107
casra
  • 431
  • 1
  • 4
  • 4

6 Answers6

37

As documented on the docker hub page for that image (https://hub.docker.com/_/mongo/) you can use

volumes:
  - './data:/data/db'

Which will use the host path ./data

dnephin
  • 25,944
  • 9
  • 55
  • 45
  • I have this ```web: build: . command: python -u app.py ports: - "5000:5000" volumes: - .:/todo links: - db db: image: mongo:3.0.2 volumes: - ./data:/data/db``` but the mongo container does not start and it appears that it is complaining that it does not have write permission on the ./data folder. it says: **2015-12-27T06:55:19.002+0000 I STORAGE [initandlisten] exception in initAndListen: 98 Unable to create/open lock file: /data/db/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating** im not sure what the problem is – casra Dec 27 '15 at 06:56
  • The problem is the file permissions. Mongo probably runs as the mongo user, which doesn't have the same permissions as your user. – dnephin Dec 27 '15 at 23:26
  • but the ** /data/db/mongod.lock** is inside the container, not on my host machine. Doesnt this **volumes: - ./data:/data/db* mean the **/data/db** of the container is going to be same as ** ./data ** of the host machine? – casra Dec 28 '15 at 01:40
  • and just in case the problem is with the permission as you stated: "mongo from inside the container not having permission to write on the local disk"; how can i fix this? if this is the case, then what is the standard way of specifying user permissions using the yml file? – casra Dec 28 '15 at 03:12
  • yes, that volume config tells the container to use a host path for the `/data/db` path in the container. You could move the lock file with a mongo config change maybe, but maybe it is supposed to be in the same path as the data itself. You can fix the permissions with `chown` or `chmod` on the host. The user id needs to match the mongo user id in the container, or you could give all users `+wx` in the directory. – dnephin Dec 28 '15 at 18:51
  • I tried `chmod +wx ./data/` on the local folder but still get the same problem. The mongo container does not start – casra Dec 31 '15 at 08:10
  • For future reference, another alternative that can work to make writable is: volumes: - './data:/data/db:rw' At least that works for named volumes. – José Roberto Abreu Feb 24 '23 at 20:45
11

I suppose you try to start the container on an OSX system like me? The host machine volume directory cannot be under /Users (or ~) as joshuajabbour points out here.

Try for example

 volumes:
   - /usr/local/mongodb:/todo
Philiiiiiipp
  • 705
  • 1
  • 9
  • 24
0
#Mongo Dockerfile
FROM alpine:edge

MAINTAINER "loko" <binario200@gmail.com>

# proxy settings
ARG http_proxy=http://your-corporate-proxy-if-is-need-it/
ARG https_proxy=http://your-corporate-proxy-if-is-need-it/
ARG no_proxy=localhost,127.0.0.0/8,::1,15.0.0.0/8,16.0.0.0/8

ADD run /
ADD dosu /sbin/

RUN chmod +x /sbin/dosu && \
  echo http://dl-4.alpinelinux.org/alpine/edge/testing >> /etc/apk/repositories && \
  apk add --no-cache mongodb

VOLUME /data/db
EXPOSE 27017 28017

ENTRYPOINT [ "/run" ]
CMD [ "mongod" ]

Docker Compose

version: '2.0'

volumes:
  data:
    external:
      name: "the-volume-name-you-want"
services:
     web:
       build:
         context: .
         dockerfile: "Dockerfile"
         args:
           - HTTP_PROXY
           - HTTPS_PROXY
           - http_proxy
           - https_proxy
           - no_proxy
           - NO_PROXY
       image: "docker-hub-OR-your-built-image-name"
       environment:
          - http_proxy=$http_proxy
          - https_proxy=$https_proxy
          - no_proxy=$no_proxy
          - HTTP_PROXY=$HTTP_PROXY
          - HTTPS_PROXY=$HTTPS_PROXY
          - NO_PROXY=$NO_PROXY
       ports:
         - "8080"
       restart: always
       depends_on:
         - mongo
     mongo:
       image: "your-favorite-mongodb-image-name"
       environment:
          - http_proxy=$http_proxy
          - https_proxy=$https_proxy
          - no_proxy=$no_proxy
          - HTTP_PROXY=$HTTP_PROXY
          - HTTPS_PROXY=$HTTPS_PROXY
          - NO_PROXY=$NO_PROXY
       restart: always
       volumes:
         - data:/data/db

build and run

docker-compose build .
docker-compose up
Yılmaz Durmaz
  • 2,374
  • 12
  • 26
Victor R Hdez
  • 127
  • 1
  • 5
0

Using your file

web:
  build: .
  command: python -u app.py
  ports:
    - "5000:5000"
  volumes:
    - .:/todo # HERE --> /mnt/c/temp/mongo:/data/db (mnt= root | c = your drive | temp and mongo = folder
  links:
- db
db:
  image: mongo:3.0.2
  • Then, the value can be /mnt/c/temp/mongo:/data/db

Dont forget to use the environment variables:

environment:
  MONGO_INITDB_ROOT_USERNAME: root
  MONGO_INITDB_ROOT_PASSWORD: fw324fe
Felipe Augusto
  • 1,341
  • 1
  • 16
  • 18
0

I had the same problem, i'm using docker with windows 10 and using WSL 2 to integrate with ubuntu, i was only able to solve the problem by putting the full path:

 volumes:
          - /home/you_user/mongoDocker/docker:/data/db
0

this documentation has helped me: https://docs.docker.com/compose/compose-file/#volumes

To reuse a volume across multiple services, a named volume MUST be declared in the top-level volumes key.

This example shows a named volume (db-data) being used by the backend service, and a bind mount defined for a single service.

services:
  backend:
    image: awesome/backend
    volumes:
      - type: volume #this is to declare that the type is volume
        source: db-data #this is the name of your already existing volume
        target: /data #this is the target or destination of the data being saved by your volume 
        volume:
          nocopy: true #The nocopy modifier is for when you are creating a volume and data already exists in the container's path, you can specify if you want that data copied when the volume is created.

volumes:
  db-data:
    external: true # the external flag allows you to use volumes created outside the scope of the docker-compose file
HibaHasan
  • 1,255
  • 2
  • 5
  • 12