动因
是这样,笔者最近满怀欣喜入手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>