I am running a Docker container on my computer. This container contains a Java application that starts a server listening on 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. My docker run
command looks like this:
docker run -p 9000 -t -i my-image
The NetworkSettings
output of docker inspect
looks like this:
"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. How can I connect to the server running in the Docker image?
If you are On OSX, that means you are using a VirtualBox VM for your docker environment.
Make sure you have forwarded your port 9000 to your actual host (the mac), in order for that port to be visible from localhost.
This is valid for the old boot2docker, or the new docker machine.
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)
(replace " boot2docker-vm
" b ythe name of your vm: see docker-machine ls
)
(use -p 9000:9000
to depend on a static port mapping)
You can try to access your container from the VirtualBox host environment. Run the container with -p 9000:9000
option:
docker run -p 9000:9000 -t -i my-image
Then run docker-machine to find out IP of docker host:
docker-machine ip default
And then access your Java application from the output of command above. For example if it is 192.168.99.100:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.