Reputation: 441
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
Upvotes: 32
Views: 61989
Reputation: 1
As of today, this the working docker compose yaml
file for using docker volume with mongo image :
version: '3.1'
services:
mongo:
image: mongo
restart: always
ports:
- 27017:27017
volumes:
- type: volume
source: mongo-data
target: /data/db
volume:
nocopy: true
# environment:
# MONGO_INITDB_ROOT_USERNAME: root
# MONGO_INITDB_ROOT_PASSWORD: example
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
# ME_CONFIG_MONGODB_ADMINUSERNAME: root
# ME_CONFIG_MONGODB_ADMINPASSWORD: example
ME_CONFIG_MONGODB_URL: mongodb://mongo:27017/
volumes:
mongo-data:
driver: local
Upvotes: 0
Reputation: 127
#Mongo Dockerfile
FROM alpine:edge
MAINTAINER "loko" <[email protected]>
# 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" ]
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
docker-compose build .
docker-compose up
Upvotes: 0
Reputation: 1493
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
Upvotes: 0
Reputation: 1
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
Upvotes: 0
Reputation: 1563
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
Dont forget to use the environment variables:
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: fw324fe
Upvotes: 0
Reputation: 753
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
Upvotes: 11
Reputation: 28140
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
Upvotes: 41