Bobcares

WeSupport

Call Us! 1-800-383-5193
Call Us! 1-800-383-5193
Call Us! 1-800-383-5193

Need Help?

Emergency Response Time custom

Our experts have had an average response time of 11.98 minutes in June 2021 to fix urgent issues.

We will keep your servers stable, secure and fast at all times for one fixed price.

Connect to Localhost from within a Docker Container

by | Jun 12, 2021

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 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(' ')]

[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.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Docker Support

Spend time on your business, not on your servers.

Managing a server is time consuming. Whether you are an expert or a newbie, that is time you could use to focus on your product or service. Leave your server management to us, and use that time to focus on the growth and success of your business.

TALK TO US Or click here to learn more.
Docker Support

Spend time on your business, not on your servers.

Managing a server is time consuming. Whether you are an expert or a newbie, that is time you could use to focus on your product or service. Leave your server management to us, and use that time to focus on the growth and success of your business.

TALK TO US Or click here to learn more.

Categories:

Tags:

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF