hanjm's blog


  • 首页

  • 标签

  • 归档

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

发表于 2018-12-16 |

在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-28 的release 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中发现有一条相关的更新日志

  • DNS name docker.for.mac.host.internal should be used instead of docker.for.mac.localhost (still valid) for host resolution from containers, since since there is an RFC banning the use of subdomains of localhost. See https://tools.ietf.org/html/draft-west-let-localhost-be-localhost-06.

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

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

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

Nginx With gRPC编译安装

发表于 2018-12-15 |

之前写过nginx HTTP2编译安装的文章, 最近想探索下nginx with gRPC support, 所以更新一下.

yum apt等包管理系统安装的软件有时候比较旧, 导致一些莫名其妙的问题. 最近在给Nginx加HTTP/2模块中, 编译时加上了--with-http_v2_module参数, 但Chrome请求发现还是不是http2, 后面发现是OpenSSL版本太低. 踩过这一坑后, 感觉Linux下部分软件最好还是自己编译安装比较妥, 如果编译过程出错, 搜下错误信息, 一般是基础依赖没有安装, 很好解决.

官方的源码编译指南
https://nginx.org/en/docs/configure.html
https://nginx.org/en/docs/http/ngx_http_v2_module.html (这里写了需要OpenSSL1.0.2以上版本), 很多选项都有合适的默认值, 比如–prefix=/usr/local/nginx, 所以只需要指定自己需要的字段

--user=www-data // 习惯将web相关的服务以www-data用户运行, 如没有此用户可以创建一个也可不加此项按默认nobody用户
--group=www-data
--with-http_v2_module // 默认选项不带http2
--with-http_ssl_module // 默认选项不带ssl, 上http2必须要上ssl的
--with-stream // https://nginx.org/en/docs/stream/ngx_stream_core_module.html
--with-openssl // 指定OpenSSL
--with-pcre=./pcre-8.40 // 需要(version 4.4 — 8.40)的pcre,注意Nginx不支持pcre2
--with-pcre-jit // 打开pcre JIT支持
--with-zlib=./zlib-1.2.11 // 需要(version 1.1.3 — 1.2.11)的zlib以支持gzip

1.官网下载Nginx包

cd /usr/local
wget https://nginx.org/download/nginx-1.14.2.tar.gz
tar -zxf nginx-1.14.2.tar.gz
cd nginx-1.14.2

2.[官网下载OpenSSL 1.0.2以上版本].https://github.com/openssl/openssl/releases

cd nginx-1.14.2
wget https://github.com/openssl/openssl/archive/OpenSSL_1_1_0e.tar.gz
tar -zxf OpenSSL_1_1_0e.tar.gz

2.官网下载pcre

注意Nginx不支持pcre2,下载pcre最新版即可. 解压到Nginx解压的目录

cd nginx-1.14.2
wget https://ftp.pcre.org/pub/pcre/pcre-8.40.tar.gz
tar -zxf pcre-8.40.tar.gz

4.官网下载zlib(version 1.1.3 — 1.2.11)

cd nginx-1.14.2
wget http://zlib.net/zlib-1.2.11.tar.gz
tar -zxf zlib-1.2.11.tar.gz

5.编译并安装

./configure \
--user=nginx \
--group=nginx \
--conf-path=/etc/nginx/nginx.conf \
--with-http_v2_module \
--with-http_ssl_module \
--with-stream \
--with-openssl=./openssl-OpenSSL_1_1_0e \
--with-pcre=./pcre-8.40 --with-pcre-jit \
--with-zlib=./zlib-1.2.11
make && make install

6.为了方便操作,软链/usr/local/nginx/sbin/nginx到/usr/local/bin

ln -sf /usr/local/nginx/sbin/nginx /usr/local/bin
123…12

hanjm

24 日志
44 标签
RSS
GitHub
Links
  • (一些有趣的博客列表)
    鸟窝
  • taozj
  • feilengcui008
  • lanlingzi
  • cizixs
  • liaoph
  • liyangliang
  • ideawu
  • legendtkl
  • 算法之道
  • surmon
  • shanshanpt
  • zddhub
  • luodw
  • xiaorui
  • TiDB
  • 谢权SELF
  • songjiayang
  • cjting
  • kingname
  • 漠然
  • xiayf
  • 40huo
  • nosuchfield
  • holys
  • icymind
  • hackjutsu
  • 流浪小猫
  • 谢龙
  • Jiajun
  • Weny
  • coldcofe
  • 张俊杰的博客
  • v2fw
  • wudaijun
  • sanyuesha
© 2016 — 2019 hanjm
由 Hexo 强力驱动