简体   繁体   English

如何将 docker-machine 端口转发到 OSX 上的主机端口?

[英]How do I forward a docker-machine port to my host port on OSX?

I'm delivering a private docker container in my company and want my colleagues to be able to access in our internal network, the problem is that my guest OS is OSX and as so I can only access my application using the 192.168.99.100:3000 default ip from docker machine.我正在我的公司提供一个私有 docker 容器,并希望我的同事能够访问我们的内部网络,问题是我的客户操作系统是 OSX,因此我只能使用 192.168.99.100:3000 访问我的应用程序来自 docker 机器的默认 ip。

How can I forward the docker-machine 3000 port to my host 80 port?如何将 docker-machine 3000 端口转发到我的主机 80 端口?

At this time Docker Machine is a virtual machine running under VirtualBox in your machine, so to expose your application port you need to map your virtual machine port to your host port.此时 Docker Machine 是您机器中运行在 VirtualBox 下的虚拟机,因此要公开您的应用程序端口,您需要将您的虚拟机端口映射到您的主机端口。

To achieve this there are two options, but before make sure your Docker Machine is stopped running:要实现这一点,有两个选项,但在确保您的 Docker 机器停止运行之前:

docker-machine stop default     # see PS below if docker machine isn't default

Option 1 - Use the VirtualBox interface选项 1 - 使用 VirtualBox 界面

  • Open VirtualBox Manager打开 VirtualBox 管理器
  • Select your Docker Machine VirtualBox image (eg: default)选择您的 Docker Machine VirtualBox 映像(例如:默认)
  • Open Settings -> Network -> Advanced -> Port Forward打开设置->网络->高级->端口转发
  • Add your app name, the desired host port (eg: 80) and your Guest port (eg: 3000)添加您的应用名称、所需的主机端口(例如:80)和您的访客端口(例如:3000)

Option 2 - Use the VirtualBox command line选项 2 - 使用 VirtualBox 命令行

Just run the following command with your own parameters:只需使用您自己的参数运行以下命令:

VBoxManage modifyvm "dev" --natpf1 "myapp,tcp,,80,,3000"

Final considerations最后的考虑

Now you can start your Docker Machine running:现在您可以启动 Docker Machine 运行:

docker-machine start default
eval $(docker-machine env default)

Then just start your application Docker container and test it running http://localhost/ .然后只需启动您的应用程序 Docker 容器并运行http://localhost/对其进行测试。

PS: Your Docker Machine name may not be default , in this case change the name accordingly. PS:您的 Docker Machine 名称可能不是default ,在这种情况下,请相应地更改名称。

This can be achieved with ssh port forwarding:这可以通过 ssh 端口转发来实现:

ssh -L 0.0.0.0:80:localhost:3000 docker@$(docker-machine ip)

It will ask you for the docker user's password, which should be tcuser .它会询问你docker用户的密码,应该是tcuser

If your docker-machine instance is not named "default" then you'll have to specify its name in there like如果您的 docker-machine 实例未命名为“default”,那么您必须在其中指定其名称,例如

ssh -L 0.0.0.0:80:localhost:3000 docker@$(docker-machine ip <name>)

If you are trying to run the bulletinboard example using the following ports如果您尝试使用以下端口运行公告板示例

docker run --publish 8000:8080 --detach --name bb bulletinboard:1.0

On macOs you can open VirtualBox and then right-click on the machine --> Settings --> Network --> Advanced --> Port Forwarding在 macOS 上,您可以打开 VirtualBox,然后右键单击机器 --> 设置 --> 网络 --> 高级 --> 端口转发

If you add the following rule如果添加以下规则

在此处输入图像描述

Then you should be able to access the application using然后您应该能够使用

> http://localhost:8100/

docker-machine uses VM underneath, usually VirtualBox. docker-machine在下面使用 VM,通常是 VirtualBox。

You can find the IP address of that machine by:您可以通过以下方式找到该机器的 IP 地址:

docker-machine ip

You can access that IP directly:您可以直接访问该 IP:

docker run -p 8080:8080 apache --name hello
curl $(docker-machine ip):8080/index.html

Unfortunately that IP address is not permanent (could change after VBox restarts): port forwarding to localhost could make it permanent.不幸的是,IP 地址不是永久的(在 VBox 重新启动后可能会更改):端口转发到localhost可以使其永久化。 You have stop VM and configure VM:您已停止 VM 并配置 VM:

VBoxManage modifyvm "default" --natpf1 "myapp,tcp,,80,,3000"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM