“docker.for.linux.localhost”?! — Docker connect to host machine from a docker container in Linux

Syed Sirajul Islam Anik
3 min readNov 9, 2020

--

Image from: https://i.pinimg.com/originals/4e/73/01/4e7301538895cdc19b0eb5f2a3b60730.png

Suppose you have two microservices which communicate with each other and they reside in different network. Your microservice A has a PHP container. It needs to communicate with another microservice that is microservice B. They both reside in your host machine. How’d you do it?

On macOS, if you require communicating with your host machine from a docker container, you can just use docker.for.mac.localhost and use the port number. But I personally looked for the same in Linux, didn’t find anything. I don’t know how the docker.for.mac.localhost works. Found a few solutions for Linux. Check the solution below. And of course, I used docker-compose. The docker client has --add-host parameter which does the same thing for the following solution. Didn’t use, can’t make sure.

NOTE: I may misuse the network terms. To err is human. Let me know if I do so.

The solution

For each docker-compose‘s services, you can pass extra_hosts to the container. That does the hostname mapping. We’ll use it. Before that, we need to know what’s the docker’s gateway in our host machine.

If you run ifconfig in your terminal (install it if you don’t have it already), it’ll show you the network interface configuration. If you scroll through the output, you’ll find the docker0 and it shows IP. For my scenario, it’s 176.10.10.10. This is the gateway IP for the docker in my host machine.

docker’s gateway

Now, we need to grab the IP and store it in a variable. To do that, we can use the following bash command.

# Can use any of the following
# 1
ifconfig | grep -A 1 docker0 | awk 'NR > 1 {print $2}'
# 2
ifconfig | awk '/docker0/{getline; print}' | awk '{ print $2 }'

Try playing with the above commands. We’re trying to get the values relative to docker0‘s match. Both the above commands do the same.

Next, we need to store it in a variable. To do this we can use the following command.

export DOCKERHOST=$(ifconfig | awk '/docker0/{getline; print}' | awk '{ print $2 }')

Here, DOCKERHOST is a variable name, you can name it whatever you like. But should not violate the variable naming policy. And $(...) contains the command which will be evaluated and return the value of your docker’s gateway IP. You can replace the command within the parenthesis (...) with any of the commands written above. To store it permanently in your host machine, you can just add the above line in your ~/.bashrc or ~/.zshrc file, based on what you’ll use to run your docker-compose up command. Next, source your ~/.bashrc or ~/.zshrc file with source ~/.zshrc or source ~/.bashrc. Now, if you write echo $DOCKERHOST in your terminal, it’ll show the IP of your docker’s gateway.

Next, we’ll use this reference in our docker-compose.yml file in the extra_hosts. Thus, our docker-compose will look like something below.

version: "3"services:
application:
build:
context: .
dockerfile: local.dockerfile
ports:
- 8001:80
extra_hosts:
- "docker.for.linux.localhost:$DOCKERHOST"

So, docker.for.linux.localhost is the name you’ll use as a reference in your container. This can be anything you like to name. And on the right side of the : is the docker’s gateway IP. The $DOCKRHOST of the right side is the variable name we exported in our ~/.(bash|zsh)rc file. So, when our containers are up, from your application container, we can now use the reference docker.for.linux.localhost to connect to the host machine.

That’s all. Your containers can communicate with different docker services across your machine.

Happy coding. ❤

--

--

Syed Sirajul Islam Anik

software engineer with "Senior" tag | procrastinator | programmer | !polyglot | What else 🙄 — Open to Remote