[英]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.