简体   繁体   English

Docker-主机上的代理容器端口

[英]Docker - proxy container port on host

I have an Ubuntu VM running on my windows machine (I'm using Vagrant with VirtualBox for that). 我在Windows机器上运行了一个Ubuntu VM(为此我将Vagrant与VirtualBox一起使用)。 I am running two docker containers in the VM, one a DB the other a web server. 我在VM中运行两个docker容器,一个是DB,另一个是Web服务器。 I want to proxy the web container through the host so that I can browse the web container from the windows machine. 我想通过主机代理Web容器,以便可以从Windows机器浏览Web容器。

Does docker help with this or do I need something like HAProxy on the VM? 码头工人对此有帮助吗?还是在VM上需要HAProxy之类的东西?

There are differents ways to achieve this. 有多种方法可以实现此目的。

Lets firt assume you have the following container running on your Docker host: 让我们假设您在Docker主机上运行了以下容器:

docker run -d -p 80:80 tutum/hello-world

The -p option tells Docker to open port 80 on the Docker host and forward traffic to port 80 of the Docker container. -p选项告诉Docker打开Docker主机上的端口80并将流量转发到Docker容器的端口80

By assigning static IP to your Vagrant box 通过为您的Vagrant框分配静态IP

In your Vagrantfile , you can assign a fixed IP to your Vagrant box by adding: Vagrantfile中 ,您可以通过添加以下内容为Vagrant框分配固定的IP:

config.vm.network "private_network", ip: "176.16.0.3"

Then from Windows, open http://176.16.0.3/ 然后从Windows打开http://176.16.0.3/

By forwarding a port in your Vagrant box 通过在“游民游”框中转发端口

If you don't want to assign a fixed IP address to your Vagrant box, you can instead forward port 80 from the Ubuntu box to port 80 of the Vagrant host (the Windows machine). 如果您不想为Vagrant框分配固定的IP地址,则可以将端口80从Ubuntu框转发到Vagrant主机(Windows计算机)的端口80。

In your Vagrantfile , put 在您的Vagrantfile中

config.vm.network "forwarded_port", guest: 80, host: 80

Now, from the Windows machine, you can reach the webserver at http://localhost/ . 现在,从Windows计算机上,您可以通过http:// localhost /到达Web服务器。

other considerations 其他考虑

Note that in your Docker container, your webserver MUST accept connections from the outside. 请注意,在您的Docker容器中,您的网络服务器必须接受来自外部的连接。 In other words you need to bind to the special 0.0.0.0 network interface instead of just localhost or 127.0.0.1 . 换句话说,您需要绑定到特殊的0.0.0.0网络接口,而不仅仅是localhost127.0.0.1

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

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