Docker Community Forums

Share and learn in the Docker community.

Network param for connecting to 127.0.0.1 in container

I am running an app in my container that provides console access from 127.0.0.1 port 9000. To access within the container I type “telnet 127.0.0.1 9000” and it works fine.
I understand that I need to expose the port but whats the method to pair the external address with the internal localhost address ?
–Craig

Read about the EXPOSE command or -p runtime option.

Right, and if we further RTFM the question is not asking to expose the port but to provide address mapping to the internal IP address of 127.0.0.1, which is not bound to the network container, AFAIK.

https://docs.docker.com/userguide/dockerlinks/ shows an example of mapping the localhost, or 127.0.0.1 address from the docker HOST, for example

$ docker run -d -p 127.0.0.1:80:5000 training/webapp python app.py,

and my question is, based on testing is:

Is there a way to specify -p HostPort:ContainerIP:ContainerPort?
The error issued when attempted seems to show that the format is only from the host side of IP:HostPort:ContainerPort.

I just tried, I don’t think you can expose loopback, you are going to need to expose your service on eth0

Hi Craig,
Did you manage to do it?
I thought about port forwarding inside Docker, but I can’t manage to do it…

I finally found a way to do it.
You can use socat like this:
socat -d tcp-listen:1234,reuseaddr,fork tcp:127.0.0.1:6789 &

As you guess, 1234 is the port you want to expose to the outside world and 6789 is your internal port listening on localhost only.