简体   繁体   English

如何连接到在 Docker 容器中运行的服务器?

[英]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 inspectNetworkSettings输出如下所示:

"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:9000localhost:32768172.17.0.2:9000172.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.这对旧的 bo​​ot2docker 或新的 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:

http://192.168.99.100:9000 http://192.168.99.100:9000

暂无
暂无

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

相关问题 如何连接到在Mac上其他主机上运行的Docker容器? - How do I connect to a docker container running on a different host on a mac? 如何连接到OSx上的Docker容器中运行的postgres? - How do you connect to postgres running in a Docker container on OSx? 使用Mopidy docker容器运行MPD服务器,如何在Mac上播放? - Running MPD server with Mopidy docker container, how do I play on Mac? 如何从命令行连接到在非默认端口上的 Docker 容器中运行的 Postgres? - How can I connect to Postgres running in a Docker container on a non-default port from the command line? 如何从Mac的Redis Desktop Manager连接到Docker容器中的Redis服务器? - How can I connect to Redis server in a Docker Container from Redis Desktop Manager in Mac? 如何连接到Mac上Docker(1.12.0)容器中的Postgres服务器? - How to connect to a Postgres server in a Docker (1.12.0) container on a Mac? 无法连接到在Docker容器中运行的oracledb - can not connect to oracledb running in a docker container Jetty Docker 容器运行 spring 应用程序无法连接到 mysql 容器外运行 - Jetty Docker container running spring application unable to connect to mysql running outside docker container 如何访问在docker容器中运行的nginx Web服务器所服务的网页 - How to access web page served by nginx web server running in docker container Mac:如何从其他计算机远程访问在docker容器内运行的服务器? - Mac: how to remotely access server running inside docker container from other computers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM