如何使用 docker-compose 和 docker-machine 为 mongoDB 挂载外部卷

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34390220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:35:19  来源:igfitidea点击:

How to mount external volume for mongoDB using docker-compose and docker-machine

mongodbdockerdocker-composedocker-machine

提问by casra

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

我想将mongoDB数据保存在容器外部和指定的卷上。我正在使用 docker-compose 并且 yml 文件看起来像

web:
  build: .
  command: python -u app.py
  ports:
    - "5000:5000"
  volumes:
    - .:/todo
  links:
    - db
db:
  image: mongo:3.0.2

回答by dnephin

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

如该图像的 docker hub 页面上所述(https://hub.docker.com/_/mongo/),您可以使用

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

Which will use the host path ./data

哪个将使用主机路径 ./data

回答by Philiiiiiipp

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.

我想您尝试在像我这样的 OSX 系统上启动容器?正如 joshuajabbour 在此处指出的那样,主机卷目录不能位于 /Users(或 ~)下。

Try for example

尝试例如

 volumes:
   - /usr/local/mongodb:/todo

回答by Victor R Hdez

#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" ]

# Docker Compose

# Docker 撰写

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