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

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

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

  1. web:
  2. build: .
  3. command: python -u app.py
  4. ports:
  5. - "5000:5000"
  6. volumes:
  7. - .:/todo
  8. links:
  9. - db
  10. db:
  11. image: mongo:3.0.2
yduiuuwa

yduiuuwa1#

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

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

它将使用主机路径./data

cwxwcias

cwxwcias2#

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

  1. volumes:
  2. - /usr/local/mongodb:/todo
ryevplcw

ryevplcw3#

  1. # Mongo Dockerfile
  2. FROM alpine:edge
  3. MAINTAINER "loko" <binario200@gmail.com>
  4. # proxy settings
  5. ARG http_proxy=http://your-corporate-proxy-if-is-need-it/
  6. ARG https_proxy=http://your-corporate-proxy-if-is-need-it/
  7. ARG no_proxy=localhost,127.0.0.0/8,::1,15.0.0.0/8,16.0.0.0/8
  8. ADD run /
  9. ADD dosu /sbin/
  10. RUN chmod +x /sbin/dosu && \
  11. echo http://dl-4.alpinelinux.org/alpine/edge/testing >> /etc/apk/repositories && \
  12. apk add --no-cache mongodb
  13. VOLUME /data/db
  14. EXPOSE 27017 28017
  15. ENTRYPOINT [ "/run" ]
  16. CMD [ "mongod" ]

Docker编写

  1. version: '2.0'
  2. volumes:
  3. data:
  4. external:
  5. name: "the-volume-name-you-want
  6. services:
  7. web:
  8. build:
  9. context: .
  10. dockerfile: "Dockerfile"
  11. args:
  12. - HTTP_PROXY
  13. - HTTPS_PROXY
  14. - http_proxy
  15. - https_proxy
  16. - no_proxy
  17. - NO_PROXY
  18. image: "docker-hub-OR-your-built-image-name"
  19. environment:
  20. - http_proxy=$http_proxy
  21. - https_proxy=$https_proxy
  22. - no_proxy=$no_proxy
  23. - HTTP_PROXY=$HTTP_PROXY
  24. - HTTPS_PROXY=$HTTPS_PROXY
  25. - NO_PROXY=$NO_PROXY
  26. ports:
  27. - "8080"
  28. restart: always
  29. depends_on:
  30. - mongo
  31. mongo:
  32. image: "your-favorite-mongodb-image-name"
  33. environment:
  34. - http_proxy=$http_proxy
  35. - https_proxy=$https_proxy
  36. - no_proxy=$no_proxy
  37. - HTTP_PROXY=$HTTP_PROXY
  38. - HTTPS_PROXY=$HTTPS_PROXY
  39. - NO_PROXY=$NO_PROXY
  40. restart: always
  41. volumes:
  42. - data:/data/db

生成并运行

  1. docker-compose build .
  2. docker-compose up
展开查看全部
mv1qrgav

mv1qrgav4#

使用文件

  1. web:
  2. build: .
  3. command: python -u app.py
  4. ports:
  5. - "5000:5000"
  6. volumes:
  7. - .:/todo # HERE --> /mnt/c/temp/mongo:/data/db (mnt= root | c = your drive | temp and mongo = folder
  8. links:
  9. - db
  10. db:
  11. image: mongo:3.0.2
  • 然后,该值可以是/mnt/c/temp/mongo:/data/db

不要忘记使用环境变量:

  1. environment:
  2. MONGO_INITDB_ROOT_USERNAME: root
  3. MONGO_INITDB_ROOT_PASSWORD: fw324fe
展开查看全部
zf2sa74q

zf2sa74q5#

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

  1. volumes:
  2. - /home/you_user/mongoDocker/docker:/data/db
oxalkeyp

oxalkeyp6#

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

  1. services:
  2. backend:
  3. image: awesome/backend
  4. volumes:
  5. - type: volume #this is to declare that the type is volume
  6. source: db-data #this is the name of your already existing volume
  7. target: /data #this is the target or destination of the data being saved by your volume
  8. volume:
  9. 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.
  10. volumes:
  11. db-data:
  12. external: true # the external flag allows you to use volumes created outside the scope of the docker-compose file

相关问题