JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM > Docker >

Get the IP address of the Docker host from inside a Docker container

Author:JIYIK Last Updated:2025/03/25 Views:

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

Applications can run in two environments: a local host or a remote server such as AWS. These applications use the host's IP address to access the application or container running on it.

Since we usually develop and run applications on the local host, this tutorial will not cover applications running in a production environment. In this tutorial, we will see 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. In the window that opens, select Empty Project and change the project name from untitled to docker-host-ip or any name you like.

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

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

<!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 in your development environment to generate a text containing 10 sentences.


Defining an image

Create a file called Dockerfile, and then 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 the custom image is created using subsequent instructions.
  2. COPY - This will copy all 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 the image and running containers from it.


Build Nginx image

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

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

This command executes the instructions of the Dockerfile sequentially. You can watch the process 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

Running the container

In this section, we want to view the IP address of the host machine from within a running container. Since we don't have a running container, we first need to run one 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 for post 80 on the container. To view the contents of the application, make http://localhost:8000/a request to localhost:8000 ( ) on your 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 commands in a running container, we bash into the Docker container using the following command.

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

Output:

/ #

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

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

Output:

172.17.0.1

请注意, 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, rather than an IP address on the network the host is connected to.


Summarize

In this article, we learned to get the IP address of the Docker host from inside a container using the Nginx application. Note that there are other ways to achieve the same goal depending on where the container is running, and we should be free to use any method that meets our requirements.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Copy files from host to Docker container

Publish Date:2025/03/25 Views:126 Category:Docker

This article will discuss and demonstrate methods we can use to transfer files from the host to a running container in Docker. docker cp Copy the file from the host to the Docker container using docker cp The command is one of the simplest

Get the IP address of the Docker container

Publish Date:2025/03/25 Views:102 Category:Docker

This article demonstrates how to get the IP address of a Docker container. Connect to the Bridge network and get the IP address of the Docker container One of the big reasons why docker containers are so convenient is that we can easily con

Uninstalling Docker on macOS

Publish Date:2025/03/25 Views:95 Category:Docker

Recently, we have seen widespread adoption of Docker as the ultimate containerization platform. Because of this, setting up Docker on all platforms has been greatly simplified, including macOS and Windows. However, some users usually face p

Enter the Docker container's shell

Publish Date:2025/03/25 Views:98 Category:Docker

This article will demonstrate how to enter the Docker container shell using multiple methods. Use docker exec to enter the Docker container's shell We need to have a container up and running to use this command. We can check the status of t

Listing containers in Docker

Publish Date:2025/03/25 Views:122 Category:Docker

This article will discuss various commands for listing containers created in our system. This means we should create some containers beforehand for these commands to return values. List all running containers in Docker We will start by list

Mount the host directory into the Docker container

Publish Date:2025/03/25 Views:189 Category:Docker

Docker provides, among other features, tools to work with the host operating system and the container file system. One of these features is the ability to persist data in containers and share data between containers by mounting directories

Docker build command with multiple parameters

Publish Date:2025/03/25 Views:138 Category:Docker

docker build The command allows us to create a Docker image from a Dockerfile. It also allows us to build an image from a context that references a set of files located in a location specified by a PATH or URL. A Dockerfile, on the other ha

The difference between CMD and ENTRYPOINT in Docker

Publish Date:2025/03/25 Views:141 Category:Docker

Docker containers have become the standard when it comes to managing software and dependencies in different environments. When working with real-world applications, you need to create a docker file before building your application container

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial