如何从主机外部连接到docker容器(同一网络)[Windows] [英] How to connect to a docker container from outside the host (same network) [Windows]

查看:2613
本文介绍了如何从主机外部连接到docker容器(同一网络)[Windows]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了我的第一个docker容器,它使用Go运行服务器,但我无法从主机外部访问它。我刚刚开始停靠码头,所以我有点迷失在这里。



所以我有一个非常简单的Go代码启动服务器,我已经建立了docker图像它安装Go并在Linux基础映像中构建代码。我在8080端口上运行服务器,所以我将该端口暴露给运行容器的主机,如下所示:

  docker run  - p 8080:8080 dockertest 

这样工作,我可以通过docker的机器IP访问服务器(发起Docker快速入门终端时出现的),问题是我无法从主机外部访问我所托管的网站,所以如果我尝试在手机上打开相同的IP地址,它只会给我一个错误:此网页不可用(ERR_CONNECTION_TIMED_OUT)。



我也试过指定IP这样:

  docker run -p 192.168.0.157:8080:8080 dockertest 

但是当我这样做时,我无法通过Docker机器的IP和上面的命令行上指定的IP访问该网站。
我也不确定哪个IP我想在该命令中写入我使用我的电脑的IP,我也尝试了127.0.0.1(localhost),但是给了我相同的结果:无法访问该网站通过任何IP。



我已经google了这个问题,发现了很多stackoverflow问题,但没有帮助我解决我的问题,大多数是面向Linux或Mac所以解决方案不适用于我的情况。



此外,我可以在我的电脑上运行Go代码,并通过我的同一个网络从另一台设备访问该网站电脑的IP我不明白为什么当我在docker机器上运行它时,我无法访问它,这对我来说可能与IP转发有关,但我是一个完整的网络连接,主要是一个网页开发者,几乎没有任何经验。

解决方案

TL; DR 您的VirtualBox主机的网络模式 - 如果您希望在本地网络上可以访问虚拟机(和Docker容器),应该是桥接






听起来你的混乱在于连接到哪个主机以通过HTTP访问您的应用程序。您没有真正阐明您的配置是什么 - 根据您的标签中有Windows和VirtualBox的事实,我将作出一些猜测。



我猜你的Docker运行在Windows主机上的VirtualBox中运行的某种Linux操作系统。我要标记IP地址如下:



D = Docker容器的IP地址



L =在VirtualBox中运行的Linux主机的IP地址



W = Windows主机的IP地址



当您在Windows上运行Go应用程序时主机,您可以连接到本地网络上的任何地方的 http:// W:8080 / 。这是因为Go应用程序绑定Windows机器上的端口8080,并且任何尝试访问端口8080的IP地址 W 将被连接。



而且这里变得更加复杂:



VirtualBox在设置虚拟机(VM)时可以配置网络几种不同模式之一。我不记得有什么不同的选择,但你想要的是桥接。在这种模式下,VirtualBox将虚拟机连接到本地网络,就像它是网络上的独立机器一样,就像插入到网络中的任何其他机器一样。在桥接模式下,虚拟机像任何其他机器一样出现在您的网络上。其他模式设置不同,机器不会在您的网络上显示。



因此,假设您为Linux主机设置了正确的网络(桥接),Linux主机将在您的本地网络(如192.168.0.x)上具有IP地址,您将可以访问您的Docker容器,位于 http :// L:8080 /



如果Linux主机设置为桥接,您可以可以从Windows主机访问,但这将取决于它的正确模式。



编辑 - 根据下面的评论,听起来很像上面描述的情况是正确的。



有一点:Docker在我的电脑(Ubuntu Linux)上的工作原理。



想象一下,我运行的命令是相同的: docker run -p 8080 :8080 dockertest 。这样做是基于 dockertest 映像启动一个新的容器,并将Linux主机(我的PC)上的端口8080转发(连接)到容器上的端口8080。 Docker设置自己的内部网络(拥有自己的一组IP地址),以便Docker守护进程通信并允许容器相互通信。所以基本上你正在做的这个 -p 8080:8080 将Docker的内部网络连接到外部网络 - 即。主机的网络适配器 - 在特定的端口。



到目前为止我有好的,现在让我们退一步,看看你的系统。您的计算机正在运行Windows - Docker目前不在Windows上运行,因此您使用的工具已在VirtualBox虚拟机中设置了一个Linux主机。当您在环境中执行 docker运行时,完全一样的事情发生 - Linux主机上的端口8080连接到容器上的端口8080。这里的最大区别是,您的Windows主机不是运行容器的Linux主机,因此这里还有一个层,它在这个层面上进行沟通,您遇到问题。



您需要的是以下两种之一:


  1. 将VirtualBox VM上的端口8080连接到端口8080 Windows主机,就像将Docker容器连接到主机端口一样。


  2. 将VirtualBox VM直接连接到本地网络,使用桥接网络模式如上所述。


如果你去第一个选项,您将能够访问 http:// W:8080 的容器,其中 W 是IP地址或Windows主机的主机名。如果您选择了第二个,您将可以访问 http:// L:8080 中的容器,其中 L 是Linux VM的IP地址或主机名。



所有这些都是更高层次的解释 - 现在你需要弄清楚如何更改VirtualBox的配置VM。这里是我无法真正帮助您的地方 - 我不知道您在Windows机器上使用什么工具来完成所有这些工作,而且我完全不熟悉在Windows上使用Docker。



如果您可以访问VirtualBox配置窗口,可以进行如下所述的更改。还有一个命令行客户端可以修改虚拟机,但我不太熟悉。



对于桥接模式(这真的是最简单的选择),关闭虚拟机,点击顶部的设置按钮,将网络模式更改为桥接,然后重新启动VM和你很好去。虚拟机应该通过DHCP在本地网络上接收IP地址,并且应该在该IP地址的网络上的其他计算机上可见。


I've created my first docker container, it's running a server using Go but I can't access it from outside the host computer. I've just started with docker so I'm a little lost here.

So I have a very simple Go code that starts a server, I have built the docker image which installs Go and builds the code in a Linux base image. I'm running the server on port 8080 so I expose that port to the host running the container like this:

docker run -p 8080:8080 dockertest

That works and I get to access the server through docker's machine IP (the one that appears on the Docker Quickstart Terminal when initiated), the problem is I can't access the website I'm hosting from outside the host, so if I try to open the same IP address on my phone it just gives me an error: This webpage is not available (ERR_CONNECTION_TIMED_OUT).

I've also tried specifying the IP like this:

docker run -p 192.168.0.157:8080:8080 dockertest

But when I do that I can't access the website through neither the docker machine's IP nor the specified IP on the command line above. I'm also not sure which IP I'm suppose to write in that command I used my computer's IP, I've also tried 127.0.0.1 (localhost) but that gave me the same result: couldn't access the website through any IP whatsoever.

I've googled this problem and found many stackoverflow questions but neither helped me solve my issue, most of them were oriented to Linux or Mac so the solution didn't apply to my situation.

Also, I can run the Go code on my computer and access the website from another device in the same network through my computer's IP. I don't understand why I can't access it when I'm running it in the docker machine, it occurred to me that it may have something to do with IP forwarding or something but I'm a complete noob in networking, I'm mostly a web developer and have almost no experience in native.

解决方案

TL;DR Check the network mode of your VirtualBox host - it should be bridged if you want the virtual machine (and the Docker container it's hosting) accessible on your local network.


It sounds like your confusion lies in which host to connect to in order to access your application via HTTP. You haven't really spelled out what your configuration is - I'm going to make some guesses, based on the fact that you've got "Windows" and "VirtualBox" in your tags.

I'm guessing that you have Docker running on some flavour of Linux running in VirtualBox on a Windows host. I'm going to label the IP addresses as follows:

D = the IP address of the Docker container

L = the IP address of the Linux host running in VirtualBox

W = the IP address of the Windows host

When you run your Go application on your Windows host, you can connect to it with http://W:8080/ from anywhere on your local network. This works because the Go application binds the port 8080 on the Windows machine and anybody who tries to access port 8080 at the IP address W will get connected.

And here's where it becomes more complicated:

VirtualBox, when it sets up a virtual machine (VM), can configure the network in one of several different modes. I don't remember what all the different options are, but the one you want is bridged. In this mode, VirtualBox connects the virtual machine to your local network as if it were a stand-alone machine on the network, just like any other machine that was plugged in to your network. In bridged mode, the virtual machine appears on your network like any other machine. Other modes set things up differently and the machine will not be visible on your network.

So, assuming you set up networking correctly for the Linux host (bridged), the Linux host will have an IP address on your local network (something like 192.168.0.x) and you will be able to access your Docker container at http://L:8080/.

If the Linux host is set to some mode other than bridged, you might be able to access from the Windows host, but this is going to depend on exactly what mode it's in.

EDIT - based on the comments below, it sounds very much like the situation I've described above is correct.

Let's back up a little: here's how Docker works on my computer (Ubuntu Linux).

Imagine I run the same command you have: docker run -p 8080:8080 dockertest. What this does is start a new container based on the dockertest image and forward (connect) port 8080 on the Linux host (my PC) to port 8080 on the container. Docker sets up it's own internal networking (with its own set of IP addresses) to allow the Docker daemon to communicate and to allow containers to communicate with one another. So basically what you're doing with that -p 8080:8080 is connecting Docker's internal networking with the "external" network - ie. the host's network adapter - on a particular port.

With me so far? OK, now let's take a step back and look at your system. Your machine is running Windows - Docker does not (currently) run on Windows, so the tool you're using has set up a Linux host in a VirtualBox virtual machine. When you do the docker run in your environment, exactly the same thing is happening - port 8080 on the Linux host is connected to port 8080 on the container. The big difference here is that your Windows host is not the Linux host on which the container is running, so there's another layer here and it's communication across this layer where you are running into problems.

What you need is one of two things:

  1. to connect port 8080 on the VirtualBox VM to port 8080 on the Windows host, just like you connect the Docker container to the host port.

  2. to connect the VirtualBox VM directly to your local network with the bridged network mode I described above.

If you go for the first option, you will be able to access the container at http://W:8080 where W is the IP address or hostname of the Windows host. If you opt for the second, you will be able to access the container at http://L:8080 where L is the IP address or hostname of the Linux VM.

So that's all the higher-level explanation - now you need to figure out how to change the configuration of the VirtualBox VM. And here's where I can't really help you - I don't know what tool you're using to do all this on your Windows machine and I'm not at all familiar with using Docker on Windows.

If you can get to the VirtualBox configuration window, you can make the changes described below. There is also a command line client that will modify VMs, but I'm not familiar with that.

For bridged mode (and this really is the simplest choice), shut down your VM, click the "Settings" button at the top, and change the network mode to bridged, then restart the VM and you're good to go. The VM should pick up an IP address on your local network via DHCP and should be visible to other computers on the network at that IP address.

这篇关于如何从主机外部连接到docker容器(同一网络)[Windows]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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