How to Get IP Address of Docker Host From Inside a Docker Container

David Mbochi Njonge Feb 02, 2024
  1. Create a New Project
  2. Define an Image
  3. Build an Nginx Image
  4. Run a Container
  5. Get the IP Address of the Docker Host From Inside a Docker Container
  6. Conclusion
How to Get IP Address of Docker Host From Inside a Docker Container

IP address means Internet Protocol address, a numerical value with the syntax 192.168.0.1. IP addresses identify a device connected to a network uniquely, and it also helps us access applications running on different devices on the internet.

Applications can run on two environments: localhost or on a remote server such as AWS. These applications use the host’s IP address to access the applications or containers running on them.

Since we usually develop and run applications on the local host, this tutorial will not cover applications running in production. In this tutorial, we will understand how to get the IP address of the Docker host from inside a Docker container.

Create a New Project

Open WebStorm IDEA and select File > New > Project. On the window that opens, select Empty Project and change the project name from untitled to docker-host-ip or use any you name prefer.

Finally, press the Create button to generate an empty project.

To test this application, we will use an Nginx that displays a web page that contains the randomly generated text. Create an index.html file and copy and paste the following code into the file.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Lorem ipsum text</title>
</head>
<body>
<p>Magna elaboraret dictumst sed quidam potenti tempus
    principes morbi. Sit libero et eruditi lectus nonumes simul.
    Dicam putent diam volumus similique ponderum sententiae maecenas mattis ridens.
    Homero iisque regione voluptatibus nullam detraxit nullam eleifend brute.
    Ligula orci dicat ac luptatum quisque appareat invidunt odio mazim.
    Reformidans impetus facilis veniam fringilla.
    Consectetur eros ferri congue latine constituto veritus persequeris dolorum felis.
    Harum felis theophrastus aliquam wisi nibh nec assueverit mi nonumes.
    Cubilia option ei ex vulputate at habeo aliquet omittantur delectus.
    Sanctus dolorem moderatius venenatis agam suscipit comprehensam imperdiet amet.</p>
</body>
</html>

To generate this text, install the Lorem plugin and press Code > Generate > Generate Text on your development environment to generate a text containing 10 sentences.

Define an Image

Create a file named Dockerfile and copy and paste the following instructions into the file.

FROM nginx:1.23.1-alpine
COPY . /usr/share/nginx/html
  1. FROM - This defines the base image on which to create our custom image using subsequent instructions.
  2. COPY - This copies all the files and directories in the current directory to the image file system destination /usr/share/nginx/html.

To learn more about using the Nginx image, the Docker hub Nginx official image provides detailed information on creating an image and running a container from it.

Build an Nginx Image

To build an Nginx image of our application, use the following command to create an image with the tag host-ip:latest.

~/WebstormProjects/docker-host-ip$ docker build --tag host-ip:latest .

This command executes the instructions of the Dockerfile sequentially. The process can be viewed from the terminal window, as shown below.

=> CACHED [1/2] FROM docker.io/library/nginx:1.23.1-alpine@sha256:082f8c10bd47b6acc8ef15ae61ae45dd8fde0e9f389a8b5cb23  0.0s
=> => resolve docker.io/library/nginx:1.23.1-alpine@sha256:082f8c10bd47b6acc8ef15ae61ae45dd8fde0e9f389a8b5cb23c374086  0.2s
=> [2/2] COPY . /usr/share/nginx/html

Run a Container

In this section, we want to see the host’s IP address from the running container. Since we do not have a running container, we first need to run a container using the following command.

~/WebstormProjects/docker-host-ip$ docker run --name host-ip-service -d -p 8000:80 host-ip:latest

This command runs a container named host-ip-service that exposes port 8000 on the host to listen on post 80 on the container. To view the contents of the application, issue a request to localhost:8000 (http://localhost:8000/) on the browser, and a web page containing random text will be displayed.

Get the IP Address of the Docker Host From Inside a Docker Container

Since we want to run a command in a running container, use the following command to bash into the Docker container.

docker exec -it host-ip-service /bin/sh

Output:

/ #

To get the IP address of the docker host on which our container host-ip-service is running, type the following command and press the Enter button on your keyboard.

/ # /sbin/ip route|awk '/default/ { print $3 }'

Output:

172.17.0.1

Note that since we are using the Docker bridge, which is the default for containers, we will get the IP address of the bridge, which is 172.17.0.1, instead of the IP address on your network where the host is connected.

Conclusion

In this tutorial, we’ve learned to get the IP address of the Docker host from inside a container by leveraging an Nginx application. Note that there are other methods to achieve the same objective depending on where the container is running, and we should be free to use any approach that meets the requirements.

David Mbochi Njonge avatar David Mbochi Njonge avatar

David is a back end developer with a major in computer science. He loves to solve problems using technology, learning new things, and making new friends. David is currently a technical writer who enjoys making hard concepts easier for other developers to understand and his work has been published on multiple sites.

LinkedIn GitHub

Related Article - Docker Container