Docker OSX:从容器连接到主机端口 [英] Docker OSX: Connect to a host port from a container

查看:361
本文介绍了Docker OSX:从容器连接到主机端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Docker容器上安装一个Flask应用程序。该应用需要通过 pip 安装一些软件包。

I am trying to install a Flask app on a docker container. The app requires some packages to be installed via pip.

我在端口9000上使用本地(主机,而不是容器)pip repo。因此,我尝试了以下内容:

I am using a local (host, not container) pip repo on port 9000. Therefore, I tried the following:

pip install -i 127.0.0.1:9000/simple my_custom_package

此调用适用于主机,但是当我在容器上运行它时,我得到:

This call works on the host, but when I run it on the container I get:

Collecting my_custom_package
Url '127.0.0.1:9000/simple/my_custom_package/' is ignored. 
    It is either a non-existing path or lacks a specific scheme.

我尝试使用 curl 似乎容器不能简单地访问主机上的端口。

I made some attempts with curl, but it seems that the container can't simply access a port on the host machine.

  • OSX 10.11.6
  • Docker version 1.12.1, build 6f9534c
  • tiangolo/uwsgi-nginx-flask image
  • I have read How to access host port from docker container, but the solution does not work for OSX.
  • Access host not vm from inside container, but the data is outdated and the solutino does not work on my system.

我接触您的OSX上的Docker容器的主机端口?

推荐答案

您可以向主机回路接口添加IP别名用作通常在本地主机上运行的任何内容的服务地址。选择你不可能在其他地方使用的私有IP地址,像10.8.8.8这样做。

You can add an IP alias to your hosts loopback interface to use as a service address for anything you normally run on localhost. Choose a private IP address your are not likely to use elsewhere, something like 10.8.8.8 will do.

添加别名

$ sudo ifconfig lo0 alias 10.8.8.8 netmask 255.255.255.255 up

检查别名是否有

$ ifconfig lo0
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
    options=3<RXCSUM,TXCSUM>
    inet6 ::1 prefixlen 128 
    inet 127.0.0.1 netmask 0xff000000 
    inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 
    inet 10.8.8.8 netmask 0xffffffff 
    nd6 options=1<PERFORMNUD>

使用别名地址。

$ curl -I http://10.8.8.8:4873
HTTP/1.1 200 OK
X-Powered-By: Unicorns/1.4.0
X-Frame-Options: deny
Content-Type: text/html; charset=utf-8
ETag: "03158871ca3bbf51e45a2c133c2176b9"
Content-Length: 8524
Vary: Accept-Encoding
Date: Tue, 04 Oct 2016 00:44:34 GMT
Connection: keep-alive

如果您的服务配置为专门监听本地主机而不是通配符/所有地址,您可能需要重新配置以便在 10.8.8.8 中进行监听。

If your service is configured to listen on localhost specifically rather than a wildcard/all addresses, you may need to reconfigure it to listen on 10.8.8.8 instead.

要永久添加别名,请创建一个文件 / Library / LaunchDaemons /com.yourname.ifconfig.10.8.8.8.plist 包含以下内容:

To add the alias permanently, create a file /Library/LaunchDaemons/com.yourname.ifconfig.10.8.8.8.plist with the following content:

<plist version="1.0">
    <dict>
        <key>Label</key>
            <string>ifconfig-10.8.8.8</string>
        <key>ProgramArguments</key>
            <array>
                <string>/sbin/ifconfig</string>
                <string>lo0</string>
                <string>alias</string>
                <string>10.8.8.8</string>
                <string>netmask</string>
                <string>255.255.255.255</string>
                <string>up</string>
            </array>
        <key>RunAtLoad</key>
            <true/>
    </dict>
</plist> 

这可以作为一般的网络解决方案。私有服务地址不需要在本地主机上托管,它可以在本地网络上的任何地方。如果您的所有私有网络都有可用的服务地址,您可以使用该配置,而不是为您的本地开发提供特殊情况。

This can work as a general network solution as well. The private service address doesn't need to be hosted on localhost, it can be anywhere on your local network. If all your private networks have that service address available you can use that config everywhere rather than having a special case for your local development.

这篇关于Docker OSX:从容器连接到主机端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆