Forward Host Port to Docker Container

Forward host port to docker container

Your docker host exposes an adapter to all the containers. Assuming you are on recent ubuntu, you can run

ip addr

This will give you a list of network adapters, one of which will look something like

3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
link/ether 22:23:6b:28:6b:e0 brd ff:ff:ff:ff:ff:ff
inet 172.17.42.1/16 scope global docker0
inet6 fe80::a402:65ff:fe86:bba6/64 scope link
valid_lft forever preferred_lft forever

You will need to tell rabbit/mongo to bind to that IP (172.17.42.1). After that, you should be able to open connections to 172.17.42.1 from within your containers.

How to access host port from docker container

When running Docker natively on Linux, you can access host services using the IP address of the docker0 interface. From inside the container, this will be your default route.

For example, on my system:

$ ip addr show docker0
7: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
link/ether 00:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
valid_lft forever preferred_lft forever
inet6 fe80::f4d2:49ff:fedd:28a0/64 scope link
valid_lft forever preferred_lft forever

And inside a container:

# ip route show
default via 172.17.0.1 dev eth0
172.17.0.0/16 dev eth0 src 172.17.0.4

It's fairly easy to extract this IP address using a simple shell
script:

#!/bin/sh

hostip=$(ip route show | awk '/default/ {print $3}')
echo $hostip

You may need to modify the iptables rules on your host to permit
connections from Docker containers. Something like this will do the
trick:

# iptables -A INPUT -i docker0 -j ACCEPT

This would permit access to any ports on the host from Docker
containers. Note that:

  • iptables rules are ordered, and this rule may or may not do the
    right thing depending on what other rules come before it.

  • you will only be able to access host services that are either (a)
    listening on INADDR_ANY (aka 0.0.0.0) or that are explicitly
    listening on the docker0 interface.


If you are using Docker on MacOS or Windows 18.03+, you can connect to the magic hostname host.docker.internal.


Lastly, under Linux you can run your container in the host network namespace by setting --net=host; in this case localhost on your host is the same as localhost inside the container, so containerized service will act like non-containerized services and will be accessible without any additional configuration.

Docker : how to (reverse) forward port to allow access to the host's localhost:PORT?

Amonst the possibilities suggested, at least on windows, the best solution is to set DISPLAY to host.docker.internal:0.0 .

docker can be lauched by setting this option directly:

   docker -e DISPLAY=host.docker.internal:0.0 <OTHERARGUMENTS>

I verified that `` connects to the localhost by lauching a netcat session.

On the windows host (using cygwin), I started:

nc -l 127.0.0.1 3000

And I could connect to it from the VM by using:

telnet host.docker.internal 3000

When starting the netcat deamon using

nc -l 169.254.11.167 3000

the telnet to the localhost did not succeed from the VM, telnet 169.254.11.167 3000 was required.

Therefore, host.docker.internal (which is 168.168.65.2 inside the VM I tested), corresponds the localhost on the Windows host (and not the local host of the WSL instance running the docker container).

How to access (mysql) host port from docker container?

I added the following to docker-compose.yml:

extra_hosts:
- "host.docker.internal:host-gateway"
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://host.docker.internal:3306/db

how to publish a docker container port to one of the random available port in the host

The nginx base image already declares EXPOSE 80 and there's no way to un-expose a port, so if you use the docker run -P or --publish-all option to publish every exposed port, it will always be published alongside your manually-exposed port.

You can use the lowercase docker run -p option with only a single port number to publish that port on an arbitrary host port instead:

docker run -p 8765 -d nginx

Since Docker containers internally won't have port conflicts with each other, you may want to just use the default HTTP port 80, matching the standard Nginx config. The --expose --publish-all combination is pretty much the only actual effect of docker run --expose, and you can get the same thing with --port; you pretty much never need the docker run --expose option.

How do I forward a docker-machine port to my host port on OSX?

At this time Docker Machine is a virtual machine running under VirtualBox in your machine, so to expose your application port you need to map your virtual machine port to your host port.

To achieve this there are two options, but before make sure your Docker Machine is stopped running:

docker-machine stop default     # see PS below if docker machine isn't default

Option 1 - Use the VirtualBox interface

  • Open VirtualBox Manager
  • Select your Docker Machine VirtualBox image (e.g.: default)
  • Open Settings -> Network -> Advanced -> Port Forward
  • Add your app name, the desired host port (e.g.: 80) and your Guest port (e.g.: 3000)

Option 2 - Use the VirtualBox command line

Just run the following command with your own parameters:

VBoxManage modifyvm "dev" --natpf1 "myapp,tcp,,80,,3000"

Final considerations

Now you can start your Docker Machine running:

docker-machine start default
eval $(docker-machine env default)

Then just start your application Docker container and test it running http://localhost/.

P.S.: Your Docker Machine name may not be default, in this case change the name accordingly.



Related Topics



Leave a reply



Submit