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

Docker deploys nginx php application

Publish Date:2025/03/26 Views:131 Category:Docker

I'm learning docker recently. I'm learning by building an nginx+php development environment example. Here I record the build process. First, give a docker-compose.yml deployment configuration file version: '3' services: nginx: container_nam

How to use Docker to image a Node.js web application

Publish Date:2025/03/26 Views:107 Category:Docker

Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at

Start a Bash terminal in a new Docker container

Publish Date:2025/03/26 Views:97 Category:Docker

Docker containers are a standard unit for packaging all the dependencies of an application, allowing us to easily run them in any environment. Containers have become very popular recently, and most developers now rely heavily on containers

Passing environment variables to containers in Docker

Publish Date:2025/03/26 Views:125 Category:Docker

This article will introduce how to pass environment variables to containers in Docker. Passing environment variables to containers in Docker using the -e and tags -env We will first see how to create environment variables and pass them to t

Install Docker using Homebrew

Publish Date:2025/03/26 Views:202 Category:Docker

There is no doubt that Docker containers have revolutionized the way we develop and deploy applications. They provide developers with the ability to package applications and dependencies in an isolated environment. Recently, we've seen wide

Enforce clean build of images in Docker

Publish Date:2025/03/26 Views:88 Category:Docker

This article discusses and demonstrates how to enforce clean builds of images in Docker. Building images in Docker We will use a simple Flask application to demonstrate this concept. my-app Create a app.py simple application named in the ho

Running a Docker instance from a Dockerfile

Publish Date:2025/03/26 Views:140 Category:Docker

Docker containers have undoubtedly become the standard unit for managing software and dependencies in different environments. When using real applications, you must create a docker file before building the container image of the application

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial