How to Connect to Localhost from within a Docker Container

I have Docker container A running a server, and container B running a client. At least for testing, both containers run on the same machine (host). The client software needs to reach out of its own container and then into the server container. The various inter-container connection mechanisms are not usable, because I don’t want to assume that the server is running in a container, although this is the case in the situation described above.

In fact, the “into the server container” part is not a problem, as that is easily taken care of by port mapping, i.e. the option -p of docker run. In other words, if the server is running natively on the host machine, the issue is about the same. In still other words, the problem is in connecting to the host machine from within a Docker container.

Unfortunately, the client software in B can not use localhost or; that will loop back into the container itself.

After some research, I figured out one solution. It turned out pretty simple. First, give the host machine’s loopback interface an alias IP address (different from The client software in container B can reach the host machine by connecting to this alias IP address directly. Since this IP may be hard to remember, docker run has an option for giving it an alias.

Step 1

If the host OS is Mac, do this:

sudo ifconfig lo0 alias

If Linux, do this:

sudo ifconfig lo:0

After this, you can check the effect by

ifconfig lo:0

which will print

lo:0      Link encap:Local Loopback
          inet addr:  Mask:
          UP LOOPBACK RUNNING  MTU:65536  Metric:1

whereas before setting the alias, the printout would be

lo:0      Link encap:Local Loopback
          UP LOOPBACK RUNNING  MTU:65536  Metric:1

(To remove the alias, do sudo ifconfig lo:0 down.)

I don’t know what’s good about; it comes from this post. Other addresses are also possible.

You will want these settings to survive system reboot, i.e. want these settings to be run at system startup. To do that, 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

Step 2

Use these options in the docker run command that launches container B:

docker run --add-host=local_host: --add-host=local: blah blah

Then, within container B, the host machine can be reached by connecting to local_host, local, or directly.

I also tried --add-host=localhost:, and using localhost from within container B worked well. But there might be caveats, as localhost is, I guess, used by various programs in the system.


I have been using a much simpler alternative for quite a while.

I don’t remember the theory, but what you need to do is type this command *within the Docker container (such as B),

$ ip route show default | awk '/default/ {print $3}'

I’m running a Linux Mint 19.03 host machine. This command with the Docker container gave me

Alternatively, the following should print the same result:

$ ip -r route list match 0/0 | cut -d' ' -f3

If the command ip does not exist in your container, install the Linux package iproute2.

Now in container B, use this IP to reach the host machine and container A. If the service is running on the host machine directly (no Docker involved), the usage is the same.

If the service is running on a remote machine (in container or not), then get the IP address of the remote machine and use it the same way.

This method does not need anything to be done to the host machine.

For convenience in Python programs, I also created 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(' ')]

(Originally written July 12, 2017; added the simpler alternative in May 2020.)

Written on May 3, 2020