1. 程式人生 > 其它 >debug記 docker: Error response from daemon: Mounts denied:

debug記 docker: Error response from daemon: Mounts denied:

今天開始尚醫通Mongodb的部分。老師一上來就給我們幾個命令,讓學生安裝mongodb

#拉取映象 
docker pull mongo:latest

#建立和啟動容器 
docker run -d --restart=always -p 27017:27017 --name mymongo -v /data/db:/data/db -d mongo

#進入容器 
docker exec -it mymongo/bin/bash 

我在執行完第二個命令後終端給我報錯:

docker: Error response from daemon: Mounts denied:
The path /data/db is not shared from the host and is not known to Docker.
You can configure shared paths from Docker -> Preferences... -> Resources -> File Sharing.
See

https://docs.docker.com/desktop/mac for more info.

執行docker ps命令查看了一下容器,反饋如下:

經過查閱終端提供的文件,發現

Use File sharing to allow local directories on the Mac to be shared with Linux containers. This is especially useful for editing source code in an IDE on the host while running and testing the code in a container. By default the /Users, /Volume, /private, /tmp and /var/

folders directory are shared. If your project is outside this directory then it must be added to the list. Otherwise you may get Mounts denied or cannot start service errors at runtime.>>>

也就是說-v 宿主機目錄|容器內目錄 中的宿主機目錄必須使用/Users, /Volue, /private, /tmp和/var之一,如果想使用其他路徑,則應當修改配置檔案。至於具體如何修改,仍可參閱https://docs.docker.com/desktop/mac ,本文不再詳細介紹。

後面我運行了以下兩個命令:

docker rm mymongo

刪除了剛才建立的命名為mymongo的容器.

然後我修改了宿主機目錄,將原來的/data/db改為/Users,重新掛載資料卷建立容器。

docker run -d --restart=always -p 27017:27017 --name mymongo -v /Users:/data/db -d mongo

執行成功!