Docker OSX: Connect to a host port from a containe

2019-05-11 07:05发布

问题:

My problem

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

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.

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

Settings

  • OSX 10.11.6
  • Docker version 1.12.1, build 6f9534c
  • tiangolo/uwsgi-nginx-flask image

What have I tried

  • 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.

How do I access host ports from a Docker container on OSX?

回答1:

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.

Configuration

Add the alias

$ sudo ifconfig lo0 alias 10.8.8.8 netmask 255.255.255.255 up

Check the alias is there

$ 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>

Use the alias address.

$ 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

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.

Startup

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.



标签: macos docker pip