Docker Machine Ip

Once in a while you may need your Docker host's IP address. Here's how to do it on Docker for Mac, Windows and Linux.

  1. Docker-machine Ip
  2. Docker-machine Ip Command Not Found
  3. Docker-machine Ip Not Found
  4. Docker Machine Ip Docker For Windows
  5. Docker Machine Ip Windows 10
  6. Docker Machine Ip Default

Enter the following command to get the IP address of the Docker Toolbox virtual machine: host docker-machine ip default 192.168.99.100 The above example outputs 192.168.99.100, but this might be a different IP address on your machine. Example # You need to find out the IP address of the container running in the host so you can, for example, connect to the web server running in it. Docker-machine is what is used on MacOSX and Windows. Firstly, list your machines.

In Docker Tip #35 I wrote about connecting to your Docker host from inside of a container but a lot of things have changed since then. Here’s a more updated version.

Docker for Mac / Docker for Windows

As of Docker v18.03+ you can use the host.docker.internal hostname to connect to your Docker host.

This could come in handy if you wanted to connect to a database that’s running on your host but isn’t running inside of a container.

I often see this use case come up when people are beginning to move their stack over into using Docker. If that’s the case you would just use host.docker.internal as your DB connection host.

Docker for Linux

There’s a couple of ways to do this, but the easiest way would be to connect over the IP address listed in your docker0 network adapter.

If you ran ip a on your Docker host you might see something similar to this:

Using the above output as an example, you could connect to your Docker host from inside of a container by using this IP address: 172.17.0.1.

If you expect that IP address might change you could go the extra mile and do something like docker container run -e 'DOCKER_HOST=$(ip -4 addr show docker0 | grep -Po 'inet K[d.]+')' ..., this way every time you run your container, it’ll have the IP address available inside the container set to the DOCKER_HOST environment variable.

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.

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 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:

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!

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.

Docker machine ip windows 10

Docker-machine Ip

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.

Docker-machine Ip Command Not Found

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

Docker-machine Ip Not Found

Hive mestastore

Hive server

Wrap up

Docker Machine Ip Docker For Windows

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.

Docker Machine Ip Windows 10

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

Docker Machine Ip Default

* Illustrations from icons8.com by Murat Kalkavan.