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

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

问题描述

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

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.

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

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

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

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

我也试过像这样指定 IP:

I've also tried specifying the IP like this:

docker run -p 192.168.0.157:8080:8080 dockertest

但是当我这样做时,我既不能通过 docker 机器的 IP 也不能通过上面命令行中指定的 IP 访问该网站.我也不确定我应该在那个命令中写哪个 IP 我使用了我计算机的 IP,我也试过 127.0.0.1 (localhost) 但这给了我相同的结果:无法通过任何任何 IP.

But when I do that I can 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 supposed 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.

我在谷歌上搜索了这个问题,发现了很多 StackOverflow 问题,但都没有帮助我解决我的问题,其中大部分是针对 Linux 或 Mac,所以该解决方案不适用于我的情况.

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.

此外,我可以在我的计算机上运行 Go 代码,并通过我计算机的 IP 从同一网络中的另一台设备访问该网站.我不明白为什么当我在 docker 机器上运行它时我无法访问它,我突然想到它可能与 IP 转发或其他东西有关,但我是网络方面的完全菜鸟,我主要是 Web 开发人员,几乎没有本地开发经验.

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 检查您的 VirtualBox 主机的网络模式 - 如果需要,它应该是 bridged本地网络上可访问的虚拟机(及其托管的 Docker 容器).

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.

听起来您的困惑在于要连接到哪个主机才能通过 HTTP 访问您的应用程序.您还没有真正说明您的配置是什么 - 我将根据您的标签中包含Windows"和VirtualBox"这一事实进行一些猜测.

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.

我猜你在 Windows 主机上的 VirtualBox 中运行了某种 Linux 版本的 Docker.我将按如下方式标记 IP 地址:

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 = Docker 容器的 IP 地址

D = the IP address of the Docker container

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

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

W = Windows 主机的 IP 地址

W = the IP address of the Windows host

当您在 Windows 主机上运行 Go 应用程序时,您可以使用 http://W:8080/ 从本地网络的任何位置连接到它.这是有效的,因为 Go 应用程序绑定了 Windows 机器上的端口 8080,任何试图访问 IP 地址 W 上的端口 8080 的人都将获得连接.

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.

这里变得更加复杂:

VirtualBox 在设置虚拟机 (VM) 时,可以以多种不同模式之一配置网络.我不记得所有不同的选项是什么,但您想要的是bridged.在这种模式下,VirtualBox 将虚拟机连接到您的本地网络,就好像它是网络上的独立机器一样,就像插入网络的任何其他机器一样.在 bridged 模式下,虚拟机像任何其他机器一样出现在您的网络上.其他模式的设置有所不同,并且机器在您的网络上将不可见.

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.

因此,假设您为 Linux 主机(bridged)正确设置了网络,Linux 主机将在您的本地网络上拥有一个 IP 地址(例如 192.168.0.x),您将能够通过 http://L:8080/ 访问您的 Docker 容器.

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

如果 Linux 主机设置为 bridged 以外的某种模式,您可能能够从 Windows 主机访问,但这将取决于究竟是什么模式.

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.

让我们稍微备份一下:这是 Docker 在我的计算机 (Ubuntu Linux) 上的工作方式.

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

假设我运行与您相同的命令:docker run -p 8080:8080 dockertest.它的作用是基于 dockertest 映像启动一个新容器,并将 Linux 主机(我的 PC)上的端口 8080 转发(连接)到容器上的端口 8080.Docker 设置自己的内部网络(使用自己的一组 IP 地址)以允许 Docker 守护程序进行通信并允许容器相互通信.所以基本上你用 -p 8080:8080 做的是将 Docker 的内部网络与外部"网络连接起来——即.主机的网络适配器 - 在特定端口上.

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.

和我在一起吗?好的,现在让我们退后一步,看看您的系统.您的机器正在运行 Windows - Docker(当前)不在 Windows 上运行,因此您使用的工具已在 VirtualBox 虚拟机中设置了 Linux 主机.当您在您的环境中执行 docker run 时,会发生完全相同的事情 - Linux 主机上的端口 8080 连接到容器上的端口 8080.这里最大的不同是你的 Windows 主机不是运行容器的 Linux 主机,所以这里还有另一层,它是跨这一层的通信,你会在那里遇到问题.

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. 将 VirtualBox VM 上的端口 8080 连接到 Windows 主机上的端口 8080,就像将 Docker 容器连接到主机端口一样.

  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.

使用我上面描述的 bridged 网络模式将 VirtualBox VM 直接连接到本地网络.

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

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

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.

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

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.

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

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.

对于bridged模式(这确实是最简单的选择),关闭你的VM,点击顶部的设置"按钮,将网络模式更改为bridgedcode>,然后重新启动 VM,您就可以开始了.VM 应该通过 DHCP 在您的本地网络上获取一个 IP 地址,并且应该可以被该 IP 地址的网络上的其他计算机看到.

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天全站免登陆