Sometimes, we may fail to Connect to Localhost from within a Docker Container.
As part of our Docker Hosting services, we assist our customers with several Docker queries.
In this article, let us see how we can connect to Localhost.
Connect to Localhost from within a Docker Container
Suppose we have Docker container A running a server, and container B running a client. For test purposes, they run on the same machine (host).
Here, the client has to reach out of its container into the server container.
However, the client software in B can not use localhost or 127.0.0.1. It will loop back into the container itself.
The solution to this is simple.
First, we need to give the host machine’s loopback interface an alias IP address. The client software in container B can reach the host machine by connecting to this alias IP address directly.
Since it can be hard to remember this IP, docker run has an option for giving it an alias.
Step 1
If the host OS is Mac, our Support Techs recommend this way.
sudo ifconfig lo0 alias 10.254.254.254
On the other hand, if it is Linux, we run:
sudo ifconfig lo:0 10.254.254.254
Then, we check the effect:
ifconfig lo:0
The output will show:
lo:0 Link encap:Local Loopback
inet addr:10.254.254.254 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
Whereas before setting the alias, it will be:
lo:0 Link encap:Local Loopback
UP LOOPBACK RUNNING MTU:65536 Metric:1
Suppose, we need to remove the alias. To do so, we run, sudo ifconfig lo:0 down
.
Then we need these settings to survive system reboot, i.e. to be run at system startup.
To do that, we put the following block (with blank lines before and after) in the file /etc/network/interfaces
:
auto lo:0
allow-hotplug lo:0
iface lo:0 inet static
address 10.254.254.254
netmask 255.255.255.0
Step 2
Moving ahead, we use these options in the docker run command that launches container B:
docker run --add-host=local_host:10.254.254.254 --add-host=local:10.254.254.254 blah blah
Then, within container B, we can reach the host machine by connecting to local_host, local, or 10.254.254.254 directly.
Update
Another alternative is to type the below command within the Docker container (such as B):
$ ip route show default | awk '/default/ {print $3}'
Suppose we run a Linux Mint 19.03 host machine. The above command with the Docker container will give 172.17.0.1.
In addition, the following will print the same result:
$ ip -r route list match 0/0 | cut -d' ' -f3
If the container does not have the ip
command, we install the Linux package iproute2
.
Then, in container B, we use this IP 172.17.0.1
to reach the host machine and container A.
With this method, we don’t have to do anything to the host machine.
For convenience in Python programs, we can run this function:
import subprocess
def get_docker_host_ip():
z = subprocess.check_output(['ip', '-4', 'route', 'list', 'match', '0/0'])
z = z.decode()[len('default via ') :]
return z[: z.find(' ')]
[Stuck with the process? We’d be happy to assist you]
Conclusion
In short, we saw how our Support Techs connect to Localhost from a Docker Container.
0 Comments