简体   繁体   English

无法使用docker-compose挂载mongodb目录卷

[英]Can't mount mongodb directory volume with docker-compose

In my main project directory I have another local directory that I store my mongodb data in which is ./pokemondb . 在我的主项目目录中,我有另一个本地目录,我存储我的mongodb数据,其中包含./pokemondb I know this directory is filled with my data because I have run mongoimport --db stats --colletion pokemon --file stats.json and can confirm the data is there in the mongo shell. 我知道这个目录充满了我的数据,因为我已经运行了mongoimport --db stats --colletion pokemon --file stats.json并且可以确认mongo shell中的数据。 I also have a docker-compose file that looks like this 我还有一个看起来像这样的docker-compose文件

pokemon-app:
  build: .
  ports:
    - "3000:3000"
  links:
    - mongo

mongo:
  image: mongo:3.2.4
  ports:
    - "27017:27017"
  volumes:
    - "./pokemondb:/data/db"

I run docker-compose up and no errors occur. 我运行docker-compose up并且没有错误发生。 But the problem is that the mongodb directory /data/db now doesn't contain the mounted volume I tried to pass. 但问题是mongodb目录/data/db现在不包含我试图传递的挂载卷。 I can confirm that the data wasn't passed correctly by executing docker exec -ti [mongo container id] bash and checking the /data/db directory with the mongo shell, indeed nothing is there. 我可以通过执行docker exec -ti [mongo container id] bash并使用mongo shell检查/data/db目录来确认数据未正确传递,实际上没有任何内容。 What am I doing wrong and why is my mongodb data directory not mounting the volume correctly? 我做错了什么,为什么我的mongodb数据目录没有正确安装音量?

EDIT : I found an unexpected solution to my problem. 编辑 :我发现了一个意想不到的问题解决方案。 One of my problems was that I had a fundamental misunderstanding of what docker volumes are. 我的一个问题是我对Docker卷的基本误解。 I previously thought that docker volumes were meant to copy data from your local machine into a docker container when it starts up. 我以前认为docker卷是为了在启动时将数据从本地机器复制到docker容器中。 But in fact docker volumes are meant to save data on your local machine generated in the docker container. 但事实上,docker卷用于保存docker容器中生成的本地计算机上的数据。 The solution the original problem I asked above was to create a dockerfile that copies the data into the image then import the data into the database when the container starts up. 我上面提到的原始问题的解决方案是创建一个dockerfile,将数据复制到映像中,然后在容器启动时将数据导入数据库。 My final docker compose file looks like this. 我的最终docker撰写文件看起来像这样。

app:
  build: .
  ports:
    - 3000:3000
  links:
    - mongodb

mongodb:
  image: mongo:3.2.4
  ports:
    - 27017:27017

stats:
  build: ./stats
  links:
    - mongodb

If you're using Docker Toolbox for OSX then only the home directory is available to the VM. 如果您正在使用Docker Toolbox for OSX,那么只有VM可以使用主目录。 If you're outside the your home directory, you need to either add another shared volume to the Virtualbox VM, or move the project under your home directory. 如果您位于主目录之外,则需要将另一个共享卷添加到Virtualbox VM,或者将项目移动到主目录下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用 docker-compose 服务的 mongoDB 进行身份验证 - Can't authenticate with mongoDB from docker-compose service 当我使用 MongoDB 和 NestJs 运行 docker-compose up 时,我无法访问 http://localhost:8080 - I can't access http://localhost:8080 when I run docker-compose up With MongoDB and NestJs 节点js docker-compose 卷为空 - node js docker-compose volume empty Docker-compose Nodejs Mongodb 不成功 - Docker-compose Nodejs Mongodb doesn't success docker-compose 中的卷安装如何工作 - How volume mounting works in docker-compose 使用docker-compose run时无法调试,但是docker-compose up可以工作 - Can't debug when using `docker-compose run`, but `docker-compose up` works 得到“ npm ERR! enoent:在docker-compose中映射卷时,没有此类文件或目录” - get “npm ERR! enoent: no such file or directory” when mapping a volume in docker-compose 我如何保证 docker 主机卷在多次 docker-compose up 和 docker-compose-down 之后有正确的权限 - how can i guarantee docker host volume have the right permission after multiple `docker-compose up` & `docker-compose-down` Docker 和 Docker Compose - 复制用于生产映像但为开发安装卷 - Docker & Docker Compose - COPY for prod image but mount volume for dev docker-compose找不到/运行start.sh - docker-compose can't find/run start.sh
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM