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 /usr/local/mongodb/datadb:/data/db -d mongo
结果一开始运行还好,后面运行mongo命令,结果报错:
Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:372:17
简直心塞塞,完全不知所措,各种百度也没有好的结果,然后玩了一下午和一晚上手机,荒废了一天。
第二天再百度,发现应该还是路径的问题,后面终于又找到一篇靠谱博文,又把/Users换成了/usr/local/mongodb/datadb,运行下面命令:
docker run -d --restart=always -p 27017:27017 --name mymongo -v /usr/local/mongodb/datadb:/data/db -d mongo
才终于解决。
总结一下最后还是靠着运气才解决的。庆幸但主要还是觉得辛苦。自学党太不容易了。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端