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

0kjbasz6  于 2022-11-02  发布在  Docker
关注(0)|答案(6)|浏览(166)

我希望将mongoDB数据保存在容器之外的指定卷上。

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

yduiuuwa1#

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

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

它将使用主机路径./data

cwxwcias

cwxwcias2#

我想你试着像我一样在OSX系统上启动容器吧?主机卷目录不能在/Users(或~)下,正如joshuajabbour指出的here
试举个例子

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

ryevplcw3#


# 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编写

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
mv1qrgav

mv1qrgav4#

使用文件

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
  • 然后,该值可以是/mnt/c/temp/mongo:/data/db

不要忘记使用环境变量:

environment:
  MONGO_INITDB_ROOT_USERNAME: root
  MONGO_INITDB_ROOT_PASSWORD: fw324fe
zf2sa74q

zf2sa74q5#

我遇到了同样的问题,我正在使用docker与windows 10和使用WSL 2与ubuntu集成,我只能通过把完整的路径来解决这个问题:

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

oxalkeyp6#

此文档帮助我:https://docs.docker.com/compose/compose-file/#volumes
要在多个服务中重用卷,必须在顶级volumes项中声明命名卷。
此示例显示了一个由后端服务使用的命名卷(db-data),以及为单个服务定义的绑定装入。

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

相关问题