在 macOS 上使用 Docker 与传统 Linux 环境有所不同,主要因为 macOS 本身并非原生支持容器化技术。以下是关键影响点及解决方案:
hyperkit
或 VirtualBox
),文件系统需通过 gRPC-FUSE
或 virtiofs
挂载,导致 I/O 性能下降。
:cached
或 :delegated
挂载选项(如 -v $(pwd):/app:cached
)。VirtioFS
)。node_modules
可通过 docker volume
隔离)。localhost
可直接访问容器端口(与 Linux 一致),但部分场景需手动配置防火墙(如 macOS 的 pfctl
)。"dns": ["8.8.8.8"]
(修改 ~/.docker/daemon.json
)。www-data
)可能与宿主机用户 UID 冲突。
docker-compose.yml
中指定用户:
yaml
services:
app:
user: "${UID:-1000}:${GID:-1000}"
--user $(id -u):$(id -g)
。bash
brew install socat
socat TCP-LISTEN:6000,reuseaddr,fork UNIX-CLIENT:\"$DISPLAY\"
docker run -e DISPLAY=host.docker.internal:0 firefox
--platform linux/amd64
)或原生 ARM 镜像(如 node:lts-slim
)。/var/lib/docker
中,访问需通过 docker run -it --privileged --pid=host debian nsenter -t 1 -m -u -n -i sh
调试。lima
的轻量级容器运行时,减少资源占用(适合命令行用户):
bash
brew install colima
colima start --cpu 4 --memory 8
遇到具体问题时,可结合 docker logs
、docker stats
和 macOS 活动监视器(Activity Monitor
)进一步诊断。