我已经在与ip 192.168.50.4
相关联的私有网络中的vagrant上启动了一个API服务器。在我的/etc/hosts
我已将此IP与api.project.dev
主机相关联。我可以连接到VM外部的api.project.dev
(例如,使用浏览器)。
此外,我已在docker上启动了一个试图连接到api.dev
上的api的应用程序:
docker run --rm -v "$PWD":/worker -w /worker yproximite/php7-cli php -r "echo file_get_contents('http://api.project.dev');"
但它无法收到回复,我有PHP Warning: file_get_contents(http://127.0.53.53): failed to open stream: Connection refused in Command line code on line 1
。与此同时,它通过ping
:
docker run --rm -v "$PWD":/worker -w /worker yproximite/php7-cli ping api.project.dev
PING api.project.dev (127.0.53.53): 56 data bytes
64 bytes from 127.0.53.53: seq=0 ttl=64 time=0.048 ms
64 bytes from 127.0.53.53: seq=1 ttl=64 time=0.074 ms
64 bytes from 127.0.53.53: seq=2 ttl=64 time=0.085 ms
64 bytes from 127.0.53.53: seq=3 ttl=64 time=0.065 ms
^C
--- api.project.dev ping statistics ---
4 packets transmitted, 4 packets received, 0% packet loss
round-trip min/avg/max = 0.048/0.068/0.085 ms
我如何使php的工作方式与ping
相同?
Vagrant:1.9.1
VirtualBox:5.1.14 r112924 (Qt5.6.2)
Docker:1.13.1, build 092cba3
macOS:Sierra 10.12.3 (16D32)
更新:我可以访问我的主机添加--add-host=api.project.dev:192.168.50.4
:
docker run --add-host=api.project.dev:192.168.50.4 --rm -v "$PWD":/worker -w /worker yproximite/php7-cli php -r "echo file_get_contents('http://api.project.dev');"
是否可以从Docker的/etc/hosts
到/etc/hosts
添加外部主机?