[英]How do I connect to a server running in a Docker container?
I am running a Docker container on my computer.我正在我的计算机上运行一个 Docker 容器。 This container contains a Java application that starts a server listening on
http://localhost:9000
.此容器包含一个 Java 应用程序,该应用程序启动一个侦听
http://localhost:9000
的服务器。 When I run this code without Docker, it is trivial to connect to the server, but I am having some trouble connect when running inside a Docker container.当我在没有 Docker 的情况下运行此代码时,连接到服务器是微不足道的,但是在 Docker 容器中运行时我在连接时遇到了一些问题。 My
docker run
command looks like this:我的
docker run
命令如下所示:
docker run -p 9000 -t -i my-image
The NetworkSettings
output of docker inspect
looks like this: docker inspect
的NetworkSettings
输出如下所示:
"NetworkSettings": {
"Bridge": "",
"SandboxID": "f9c77b7ae7804583fa60211b7d8cfeec5f154b29f9d174a4f0cdb5a8d6e41126",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"9000/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "32768"
}
]
},
"SandboxKey": "/var/run/docker/netns/f9c77b7ae780",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "10b80d9de186be6ddf9e9d3bb1945124ec1fab7774a135720569e2bf4db3306e",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"EndpointID": "10b80d9de186be6ddf9e9d3bb1945124ec1fab7774a135720569e2bf4db3306e",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02"
}
}
}
I have tried connecting to localhost:9000
, localhost:32768
, 172.17.0.2:9000
, and 172.17.0.2:32768
to no avail.我尝试连接到
localhost:9000
、 localhost:32768
、 172.17.0.2:9000
和172.17.0.2:32768
无济于事。 How can I connect to the server running in the Docker image?如何连接到在 Docker 映像中运行的服务器?
If you are On OSX, that means you are using a VirtualBox VM for your docker environment.如果您在 OSX 上,这意味着您正在为您的 docker 环境使用 VirtualBox VM。
Make sure you have forwarded your port 9000 to your actual host (the mac), in order for that port to be visible from localhost.确保您已将端口 9000 转发到您的实际主机(mac),以便从 localhost 看到该端口。
This is valid for the old boot2docker, or the new docker machine.这对旧的 boot2docker 或新的 docker 机器有效。
VBoxManage controlvm "boot2docker-vm" --natpf1 "tcp-port9000 ,tcp,,9000,,9000"
VBoxManage controlvm "boot2docker-vm" --natpf1 "udp-port9200 ,udp,,9000,,$9000
( controlvm
if the VM is running, modifyvm
is the VM is stopped) (
controlvm
如果 VM 正在运行, modifyvm
是 VM 停止)
(replace " boot2docker-vm
" b ythe name of your vm: see docker-machine ls
) (用您的虚拟机名称替换“
boot2docker-vm
”:请参阅 docker docker-machine ls
)
(use -p 9000:9000
to depend on a static port mapping) (使用
-p 9000:9000
依赖于静态端口映射)
You can try to access your container from the VirtualBox host environment.您可以尝试从 VirtualBox 主机环境访问您的容器。 Run the container with
-p 9000:9000
option:使用
-p 9000:9000
选项运行容器:
docker run -p 9000:9000 -t -i my-image
Then run docker-machine to find out IP of docker host:然后运行 docker-machine 找出 docker 主机的 IP:
docker-machine ip default
And then access your Java application from the output of command above.然后从上面命令的输出中访问您的 Java 应用程序。 For example if it is 192.168.99.100:
例如,如果它是 192.168.99.100:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.