Sometimes, we may be unable to Connect to Localhost from within a Docker Container.
Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform related Docker queries.
In this context, we shall look into how we can connect to Localhost.
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.
1.
If the host OS is Mac, We recommend you run the below command:
$ 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 look like this:
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
2.
Here, 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.
3. An alternative is to type the below command within the Docker container:
$ 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 subprocessdef 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(' ')]
This article covers method to connect to Localhost from a Docker Container. If you are using Docker-for-mac or Docker-for-Windows 18.03+, just connect to your mysql service using the host host.docker.internal (instead of the 127.0.0.1 in your connection string).
If you are using Docker-for-Linux 20.10.0+, you can also use the host host.docker.internal if you started your Docker container with the --add-host host.docker.internal:host-gateway option.