Docker:MacOSX将Container端口暴露给主机 [英] Docker: MacOSX Expose Container ports to host machine

查看:194
本文介绍了Docker:MacOSX将Container端口暴露给主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的工作中,我使用docker和选项 - net = host 的工作方式是将Docker容器端口转发到机器上。这允许我添加使用某些端口的grunt任务,例如:




  • 在端口9001中提供覆盖报告的功能

  • 在端口9000中提供我的应用程序的本地部署版本

  • 手表现场重新加载端口35729

  • 单元测试运行器使用9876端口



当我在Mac中开始使用Docker时,我遇到的第一个问题是: code> - net = host 不再工作了。
我研究过,我明白为什么这是不可能的(Docker在Mac中运行在一个自己的虚拟机中),而我的瞬间解决方案是使用 -p 选项进行暴露端口,但这个限制我添加越来越多的使用端口的任务,因为我需要为我需要暴露的每个端口运行显式的 -p 命令。



有同样问题的人吗?如何处理这个问题?

解决方案

您的问题最可能是您使用dockertoolbox或dhingy /任何其他提供一个完整的linux VM ,然后托管docker来运行您的容器在这个VM内。这个虚拟机当然有自己的网络堆栈和自己的IP在主机上,这是你的工具将有问题。容器的暴露端口不会暴露于 OSX主机本地主机,而是 OSX Docker-VM-ip 。 / p>

优雅地解决这些问题



将端口暴露给OSX localhost 从容器




  1. 首先,使用/ install docker-for-mac https://docs.docker.com/engine/installation/mac/ ,而不是dockertoolbox或其他。它基于一个特殊的xhyve堆栈,当您现在执行 docker run -p 3306:3306 percona 时,会重用您的主机网络堆栈

  2. 将绑定到$ code> osx-host-localhost 上的3306,因此尝试附加到localhost:3306的每个其他osx工具都将工作(非常有用),就像您习惯当您使用安装mysql时,您可以使用安装mysql 或同样

  3. 如果在OSX上使用docker容器的代码共享遇到性能问题,请检查 http://docker-sync.io - 它与docker-for-mac兼容(提示:我有偏见将这些端口从OSX主机导出到一个包含器






< h2>

您不会真正导出任何东西,而是使得它们可以从所有容器(OSX-host-localhost的所有端口)整体访问



如果你想附加到你提供的端口上e OSX主机,中的,例如。在xdebug会话期间,您的IDE将侦听OSX-host-localhost上的端口9000 ,并且运行FPM / PHP的容器应该附加到此osx-localhost:9000上mac,您需要执行以下操作: https://gist.github.com/EugenMayer/3019516e5a3b3a01b6eac88190327e7c



所以你创建一个虚拟环回ip,所以你可以使用10.254.254.254:9000从无容器访问你的OSX主机端口 - 这是可移植的,基本上给你所有你需要像以前一样开发






所以一个让你连接到容器暴露的端口到运行的应用程序mac和尝试连接到本地主机:port



而第二个反向,如果容器中的某些东西想附加到主机上的端口。


In my job I working with docker and the option --net=host working like a charm forwarding the docker container ports to the machine. This allows me to adding grunt tasks that use certain ports by example:

  • A taks for serving my coverage report in a port 9001
  • A local deployed version of my app served in the port 9000
  • A watch live reload the port 35729
  • For Unit testing runner use the 9876 port

When I begin to use Docker in Mac, the first problem that i had was: The option --net=host don't work anymore. I researched and I understand why this is not possible (Docker in Mac runs in a own virtual machine) and my momentary solution it's use the -p option for expose the ports, but this limit to me to add more and more task that use ports because i need run the explicit -p command for each port that i need expose.

Anyone with this same problem? How to dealing with this ?

解决方案

Your issue is most probably that you are using dockertoolbox or dhingy/dlite or anything else providing a full-fledged linux VM, which then hosts docker to run your container inside this VM. This VM has, of course, its own network stack and own IP on the host, and thats were your tools will have issues with. The exposed ports of the container are not exposed to OSX host localhost, but rather OSX Docker-VM-ip.

To solve those issues elegantly

Expose ports to OSX localhost from the container

  1. First, use/install docker-for-mac https://docs.docker.com/engine/installation/mac/ instead of dockertoolbox or others. Its based on a special xhyve stack which reuses your hosts network stack
  2. when you now do docker run -p 3306:3306 percona it will bind 3306 on the osx-host-localhost, thus every other osx-tool trying to attach to localhost:3306 will work ( very useful ) just as you have been used to it when you installed mysql using brew install mysql or likewise
  3. If you experience performance issues with code shares on OSX with docker containers, check http://docker-sync.io - it is compatible with docker-for-mac ( hint: i am biased on this one )


Export ports from the OSX-host to a containter

You do not really export anything in particular, you rather make them accessable as a whole from all containers ( all ports of the OSX-host-localhost)

If you want to attach to a port you offered on the OSX host, from within a container, e.g. during a xdebug session were your IDE listens on port 9000 on the OSX-host-localhost and the container running FPM/PHP should attach to this osx-localhost:9000 on the mac, you need to do this: https://gist.github.com/EugenMayer/3019516e5a3b3a01b6eac88190327e7c

So you create a dummy loopback ip, so you can access your OSX-host ports from without containers using 10.254.254.254:9000 - this is portable and basically gives you all you need to develop like you have used to


So one gives you the connectivity to container-exposed ports to apps running on the mac and trying to connect to localhost:port

And the second the inverse, if something in the container wants to attach to a port on the host.

这篇关于Docker:MacOSX将Container端口暴露给主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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