Docker Machine Ip

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

Jun 22, 2020 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.

If you want multiple Docker Containers to talk to each other, they can form a Bridge Network. Each Container Network has its own Subnet mask to distribute IP addresses. The default subnet for a Docker Network is 172.17.0.0/16. In this article, we are going to discuss the different ways you can use to know the IP address of a Docker Container. The following command returns the IP address of the last running container. Sudo docker exec −it $(docker ps −l −q) env grep ADDR. Using the IP address command. You can also access the IP address of the container by running the IP address command. Check out the command below. Sudo docker exec −it ip addr grep global.

May 12, 2015 docker-machine ip 192.168.99.102 If you want to configure your environment variables, needed by the client more easy, just use the following command. 10 Examples of how to get Docker Container IP Address. One thing that is so much fun about the need virtualization paradigm we are heading towards is that in the age of “software defined”, the flexibility of how to do things with infra are much more like programming then working with highly opinionated traditional infrastructure.

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-machine Ip Default Does Not Exist

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.

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

Machine

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.

  • Related Questions & Answers
  • Selected Reading
DockerOperating SystemOpen Source

We all know that we can run our application in a packaged environment called container using Docker. When you want containers to talk to each other, the network they create can be assumed to be a bridge network. Run the following command to get a list of networks.

Docker Machine Ip Default

Each network of containers has a subnet mask and can be used to distribute IP addresses to its containers. This also means that each container in the docker network is assigned an IP address. The default subnet for a docker network is 172.17.0.0/16.

Knowing these, we will now see the different methods that can be used to find out the IP address of a docker container in a network.

Docker Machine Ip Default

  • Using Docker inspect

Inspect command is used to get low level information about all the running docker containers in the host machine. It is shown in JSON format. We can use the format option with the command to get only a handful of important information.

Use the following commands below to get the container IP address using inspect.

First command is used to get a list of container IDs of all the running containers. This can be used on the second command to find the IP addresses.

  • In case of a single container

In case you only have a single container running, you can directly return the last container ID by parsing it

  • Using the bash

You can also get a container’s network ID by attaching a bash shell to the container. Use the commands below.

To start the bash of the container, use −

Once you are inside the bash, you can use the following command to access the IP address.

Docker Machine Ip
  • Exporting the environment variable of the container

For a particular docker container, you can export the environment variable. The following command returns the IP address of the last running container.

Docker Machine Ip Command

  • Using the IP address command

You can also access the IP address of the container by running the IP address command. Check out the command below.

  • Using the bashrc file

Docker-machine Ip Command Not Found

You can access the IP address of a particular container by creating your own custom command. You just need to add a method inside your bashrc file that would take an argument which will be the container ID and then return the IP address of that container. Check out the command below.

Paste the above code at the end of your ∽/.bashrc file.

Reload the file by using the command −

Now, get the container ID using the following command.

Copy the container ID and use it in the following command to get the container’s IP address.

In this article, we have seen six different ways to get a docker container’s IP address. However, the best and probably the easiest one is using the inspect command and this makes it one of the most widely used one too.