mac docker 宿主机和容器间网络打通

动因

是这样,笔者最近满怀欣喜入手Docker,看着各种文章命令都是不断点头称道:"嗯嗯,不错不错",在接下来终于准备大干一场的时候碰壁了,主要情况是说在Mac中跑了第一把的时候发现碰到,虚拟机和宿主机居然是不通的,当然也找了一下资料说linux才是天然支持docker滴,底层都有cgroup,clx吧啦吧啦,mac,windows这种其实是底层干了波虚拟机,然后要转发啥的。从我过去多次学习大数据组件入门到放弃的情况来说,网络这种事情要提前解决,不能卡着,否则后面很多破事情。找了一堆资料,自己亲测OK,而且不是很繁琐的方式,我记录下来。

办法

使用 docker-connector, 上链接

原理官网其实也说明了

动作

先是安装

bash 复制代码
brew install wenjunxiao/brew/docker-connector
bash 复制代码
docker network ls --filter driver=bridge --format "{{.ID}}" | xargs docker network inspect --format "route {{range .IPAM.Config}}{{.Subnet}}{{end}}" >> /usr/local/etc/docker-connector.conf
bash 复制代码
sudo brew services start docker-connector

然后是内部容器启动

bash 复制代码
docker run -it -d --restart always --net host --cap-add NET_ADMIN --name connector wenjunxiao/mac-docker-connector

接下来是试一把:

bash 复制代码
curl http://172.17.0.2
bash 复制代码
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
相关推荐
猫头虎6 小时前
如何在浏览器里体验 Windows在线模拟器:2026最新在线windows模拟器资源合集与技术揭秘
运维·网络·windows·系统架构·开源·运维开发·开源软件
黎陌MLing6 小时前
docker安装管理TDengine
docker·容器·tdengine
❀͜͡傀儡师8 小时前
docker部署Arcane容器可视化管理平台
运维·docker·容器
老姚---老姚8 小时前
docker常用命令
运维·docker·容器
古城小栈8 小时前
Rust 网络请求库:reqwest
开发语言·网络·rust
qq_3168377511 小时前
IP网段冲突 配置指定ip使用指定的网络接口发送,而不经过默认网关
服务器·网络·tcp/ip
布史11 小时前
Tailscale虚拟私有网络指南
linux·网络
软件小滔11 小时前
Mac 上看图?从需求出发的功能匹配
经验分享·macos·mac·应用推荐
枷锁—sha12 小时前
彻底解决 Google Gemini 报错:异常流量与 IP 地址冲突排查指南
网络·网络协议·tcp/ip
软件小滔12 小时前
MacOS 26.0 网速监控我试了十几款,从系统API到可视化实现
经验分享·macos·mac·应用推荐