Docker 无法通过纯数据容器启动带有附加卷的 MongoDb

Docker cannot start MongoDb with attached volume through data-only container

我正在尝试 运行 docker-compose 在我的 Windows 机器上旋转 MongoDB 实例和一个 data-only 容器代理包含数据库文件的附加 volume

mongodata:  
  image: mongo:2.6.8
  volumes:
    - ./data/db:/data/db
  command: --break-mongo
mongo:  
  image: mongo:2.6.8
  volumes_from:
    - mongodata
  ports:
    - "27017:27017"
  command: --smallfiles --rest

*p.s。 --break-mongo 命令是故意的,因为它只需要创建卷

据我了解,使用 data-only 卷模式可以处理权限问题,但我可以在 Mongo 容器启动期间看到以下错误:

 [0m2016-01-26T00:23:52.340+0000 [initandlisten] info preallocateIsFaster couldn't run due to: couldn't open file /data/db/journal/tempLatencyTest for writing errno:1 Operation not permitted; returning false
 [0m2016-01-26T00:23:52.341+0000 [initandlisten] Unable to remove temporary file due to: boost::filesystem::remove: Text file busy: "/data/db/journal/tempLatencyTest"
 [0m2016-01-26T00:23:52.344+0000 [initandlisten] exception in initAndListen: 13516 couldn't open file /data/db/journal/j._0 for writing errno:1 Operation not permitted, terminating

因此我无法将 MongoDb 与本地计算机的附加卷一起使用。有什么办法解决这个问题吗?

documentation 状态

If you are using Docker Machine on Mac or Windows, your Docker daemon has only limited access to your OS X or Windows filesystem. Docker Machine tries to auto-share your /Users (OS X) or C:\Users (Windows) directory. So, you can mount files or directories on OS X using.

docker run -v /Users/<path>:/<container path> ...

On Windows, mount directories using:

docker run -v /c/Users/<path>:/<container path> ...

All other paths come from your virtual machine’s filesystem. For example, if you are using VirtualBox some other folder available for sharing, you need to do additional work. In the case of VirtualBox you need to make the host folder available as a shared folder in VirtualBox. Then, you can mount it using the Docker -v flag.

基本上,要么尝试提供从您的 C:\Users 文件夹开始的完整路径,如上所示,或者如果您不能,请将主机文件夹设为 Virtualbox 中的共享文件夹。

更新

无需给出完整路径。 docker-compose 会处理的。您必须确保 docker-compose.yml 位于 Users 文件夹的内部(某处)。它不能在某个根文件夹中。如果您已经这样做了,那么您将不得不调整您的权限。只需授予该文件夹的完全权限。

更新:查看 latest Docker for Windows and MacOS X

Faster and more reliable: no more VirtualBox! The Docker engine is running in an Alpine Linux distribution on top of an xhyve Virtual Machine on Mac OS X or on a Hyper-V VM on Windows, and that VM is managed by the Docker application. You don’t need docker-machine to run Docker for Mac and Windows.

注意:如果 Windows,您需要 Windows 10 Pro 才能运行,因为 Hyper-V 不包含在其他版本中。

对于之前的Docker Toolbox,由于VirtualBox,在Windows 和OS X 上似乎根本没有解决方案。图像文档确实 states:

WARNING (Windows & OS X): The default Docker setup on Windows and OS X uses a VirtualBox VM to host the Docker daemon. Unfortunately, the mechanism VirtualBox uses to share folders between the host system and the Docker container is not compatible with the memory mapped files used by MongoDB (see vbox bug, docs.mongodb.org and related jira.mongodb.org bug). This means that it is not possible to run a MongoDB container with the data directory mapped to the host

作为解决方法,我只是在 mongo 守护程序启动之前从文件夹中复制。另外,在我的例子中,我不关心日志文件,所以我只复制数据库文件。

我在 docker-compose.yml 上使用了这个命令 command: bash -c "(rm /data/db/*.lock && cd /prev && cp *.* /data/db) && mongod" 每次停止我使用的容器之前: docker exec <container_name> bash -c 'cd /data/db && cp $(ls *.* | grep -v *.lock) /prev'

注意:/prev 设置为卷。 path/to/your/prev:/prev

另一种解决方法是使用 mongodump 和 mongorestore。

  • 在 docker-compose.yml 中:command: bash -c "(sleep 30; mongorestore --quiet) & mongod"
  • 在终端中:docker exec <container_name> mongodump

注意:我使用sleep是因为我想确保mongo启动了,这需要一段时间。

我知道这涉及手动工作等,但我很高兴至少我在我的 Windows 10 机器上得到了 mongo 和现有数据 运行,并且仍然可以继续工作我的 Macbook 当我想要的时候。

(透视