Docker For Mac Machine Ip

Docker provides the ability to package and run an application in a loosely isolated environment called a container.

I know what you might be thinking – come on, not another post explaining what Docker is, it's everywhere these days!

But don't worry, we are skipping that basic introduction. The target audience for this article should already have a basic understanding of what Docker and Containers are.

In the past with docker-machine I could run the following command to determine the IP address: docker-machine ip default This proved a convenient way to setup env vars within shell scripts so I could do things like run psql from host and access the postgres container being used by my app. Disclaimer: I realize that docker.local is supposed to be a shortcut way to avoid using IP addresses with. For Windows/Mac, you can either connect DOCKERHOST IP address. The other option is Port forwarding. In Windows, a Docker Machine is a virtual machine running under VirtualBox in your host machine. To enable Port forwarding for MySQL and phpMyAdmin, perform the following steps: Open “Oracle VM Virtual Box” Select your Docker Machine.

But have you ever wondered how to get a Docker Container IP Address?

Docker network explained


First, let's understand how the Docker network works. For that we are going to focus on the default bridge network. When you are using Docker, if you don’t specify a driver this is the type of network you are using.

The bridge network works as a private network internal to the host so containers on it can communicate. External access is granted by exposing ports to containers.

Bridge networks are used when your applications run in standalone containers that need to communicate.

In the picture above db and web can communicate with each other on a user created bridge network called mybridge.

If you’ve never added a network in Docker you should see something similar to this:

The default bridge network is listed, along with host and none. We will ignore the other two, and use the bridge network when we get to the examples.

Docker Container IP Address


By default, the container is assigned an IP address for every Docker network it connects to. And each network is created with a default subnet mask, using it as a pool later on to give away the IP addresses.

Usually Docker uses the default 172.17. 0.0/16 subnet for container networking.

Now to better understand it, we will execute a real use case.

Docker For Mac Machine Ip

Docker Example


To illustrate this, we will use a Hive and Hadoop environment, containing 5 Docker Containers.

Check out the docker-compose.yml file we are about to execute:

No one wants to read a HUGE config file, right? So here's a picture:

Docker

Much better! Now let's start up those containers:

We can see 5 containers:

Next let's check our Docker networks:

Wait a minute... there's a new network called docker-hive_default!

Docker for mac machine ip address

By default docker compose sets up a single network for your app. And your app’s network is given a name based on the “project name”, originated from the name of the directory it lives in.

So since our directory is named docker-hive, this explains the new network.

Next some examples on how to get the Docker IP Address.

How to Get A Docker Container IP Address - examples

And now that I have your attention, we are going to unveil the mystery.

1. Using Docker Inspect


Docker inspect is a great way to retrieve low-level information on Docker objects. You can pick out any field from the returned JSON in a fairly straightforward manner.

So shall we use it to get the IP Address from the dockerhive_datanode?

Didn't you say that Docker uses the default 172.17. 0.0/16 subnet for container networking? Why is the returned IP Address: 172.18.0.5 outside it?

To answer that we have to look at our network settings:

We executed this example in a Compute Engine VM, and in this test, the docker network was assigned a different subnet: 172.18.0.0/16. That explains it!

Furthermore, we can also lookup all IP Addresses inside the docker-hive_default network.

So we don't need to look up each Container's IP individually:

If you didn't notice, we used jq help to parse the Containers map object.

2. Using Docker exec

In the following example we will work with the dockerhive_namenode.

3. Inside the Docker Container

We can even find other containers' IP Addresses that are inside a container in the same network:

Data node

Hive mestastore

Hive server

Wrap up

All examples were executed in a linux distribution Compute Engine VM. If you execute them in macOS or Windows environments the sample commands might change a bit.

Also bear in mind that those IP Addresses in the examples given are internal to the sample docker-hive_default network. So if you have a use case to connect to those containers externally, you would need to use the host machine's external IP (assuming that you are exposing the containers ports correctly).
Or if you are using kubernetes, for instance, to manage your Docker containers, let it handle the IP Addresses for you kubernetes-expose-external-ip-address ?.

* Illustrations from icons8.com by Murat Kalkavan.

In the past with docker-machine I could run the following command to determine the IP address:

Mac

docker-machine ip default

This proved a convenient way to setup env vars within shell scripts so I could do things like run psql from host and access the postgres container being used by my app.

Disclaimer: I realize that docker.local is supposed to be a shortcut way to avoid using IP addresses with Docker for Mac, but for me it currently does not work when connected through VPN. I am sure that will be fixed eventually.

In the interim, and as part of my transition from docker-machine centric scripts, the question I would like guidance on is how do you get the IP address of xhyve docker instance?

Docker For Mac Machine Iphone

Cheers,
-Lyndon