7

Macos Docker container连接宿主机172.17.0.1的办法

 3 years ago
source link: https://www.imhanjm.com/2018/12/16/macos%20docker%20container%E8%BF%9E%E6%8E%A5%E5%AE%BF%E4%B8%BB%E6%9C%BA172.17.0.1%E7%9A%84%E5%8A%9E%E6%B3%95/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Macos Docker container连接宿主机172.17.0.1的办法

发表于 2018-12-16

| 3 Comments

在Linux docker container里面, 如果想访问宿主机上的服务, 用 172.17.0.1 这个host即可.

今天在Mac上的 dockercontainer里面启动一个服务, 这个服务需要连我主机上的MySQL, 用 172.17.0.1 是访问不了的, Connection refused.

root@d99939cc53fc:/tmp# curl 172.17.0.1:3306
curl: (7) Failed to connect to 172.17.0.1 port 3306: Connection refused

但是看网络结构, 和Linux的一样, 也是在172.17段下的.

root@d99939cc53fc:/tmp# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
2: tunl0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN group default qlen 1
link/ipip 0.0.0.0 brd 0.0.0.0
3: ip6tnl0@NONE: <NOARP> mtu 1452 qdisc noop state DOWN group default qlen 1
link/tunnel6 :: brd ::
6: eth0@if7: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever

不得其解, Google之, 发现有个隐藏奥秘, https://stackoverflow.com/questions/38504890/docker-for-mac-1-12-0-how-to-connect-to-host-from-container 问题下有人在 Docker Community Edition 17.06.0-ce-mac18, 2017-06-28release notes中发现有

Add an experimental DNS name for the host: docker.for.mac.localhost

这样一条更新日志.

页面搜索docker.for.mac.localhost, 发现在 Docker Community Edition 17.12.0-ce-mac46 2018-01-09 的 release notes中发现有一条相关的更新日志

所以, 结论就是在 container 中应该用 docker.for.mac.host.internal 来访问宿主机.

于是用curl看一下端口通不通, 果然通.

root@d99939cc53fc:/tmp# curl docker.for.mac.host.internal:3306
5.7.21Bf

Recommend

  • 12
    • swiftweekly.github.io 4 years ago
    • Cache

    Issue #172 05 Nov 2020

    Issue #172 05 Nov 2020 Written by: Kristaps Grinbergs The last two weeks have been very active. Swift core team shared an article explaining

  • 6
    • www.cxyxiaowu.com 4 years ago
    • Cache

    LeetCode 第 172 号问题:阶乘后的零

    LeetCode 第 172 号问题:阶乘后的零-五分钟学算法 当前位置:五分钟学算法 > LeetCodeAnimation > LeetCode 第 172 号问题:阶乘后的零 ...

  • 6
    • adbackstage.googledevelopers.libsynpro.com 4 years ago
    • Cache

    Episode 172: Privacy features in Android 12

    Privacy features in Android 12 Preview Mode Links will not work in preview mode ...

  • 7
    • swiftweeklybrief.com 3 years ago
    • Cache

    Issue #172

    Issue #172 05 Nov 2020 Written by: Kristaps Grinbergs The last two weeks have been very active. Swift core team shared an article explaining

  • 8
    • blog.lucien.ink 3 years ago
    • Cache

    docker 连接宿主机的 MySQL

    请注意,本文编写于 645 天前,最后修改于 143 天前,其中某些信息可能已经过时。 docker 连接宿主机的 MySQL本文地址:https://blog.lucien.ink/archives/505

  • 9

    Brought to you by The term “foundation” model has been around since about the middle of last year when a research group at Stanford published the comprehensive report

  • 9

    菜鸟收入172.92亿元,70%来自外部客户 原创 蓝鲸财经 杨泽世 · 2022-08-05 01:05:06 阅 3.9w 菜鸟相关工作人员向记者表示,菜鸟本季度进一步提升了面向商家和消费者的履约及增值服务,包括以直营配送...

  • 7
    • www.thetype.com 2 years ago
    • Cache

    字谈字畅 172:剑标的前世今生

    字谈字畅 172:剑标的前世今生 钱 争予 | 2022/03/01

  • 7
    • airfactsjournal.com 2 years ago
    • Cache

    The $20 an hour Cessna 172 experiment

    The $20 an hour Cessna 172 experiment Blog - Latest News The $20 an hour Cessna 172 exp...

  • 9

    8GB、16GB显存的性能差多少?实测多达172%! 2023-03-10 20:48 出处/作者:快科技 整合编辑:佚名 0 ...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK