迹忆客 EN >

当前位置:主页 > 学无止境 > 操作系统 > Docker >

从 Docker 容器内部获取 Docker 主机的 IP 地址

作者:迹忆客 最近更新:2023/04/17 浏览次数:

IP地址是指互联网协议地址,一个数值,语法为192.168.0.1。 IP地址唯一标识连接到网络的设备,它还帮助我们访问互联网上运行在不同设备上的应用程序。

应用程序可以在两种环境中运行:本地主机或远程服务器(如 AWS)。 这些应用程序使用主机的 IP 地址来访问在其上运行的应用程序或容器。

由于我们通常在本地主机上开发和运行应用程序,因此本教程不会涵盖在生产环境中运行的应用程序。 在本教程中,我们将了解如何从 Docker 容器内部获取 Docker 主机的 IP 地址。


创建一个新项目

打开 WebStorm IDEA 并选择文件 > 新建 > 项目。 在打开的窗口中,选择 Empty Project 并将项目名称从 untitled 更改为 docker-host-ip 或使用您喜欢的任何名称。

最后,按创建按钮生成一个空项目。

为了测试此应用程序,我们将使用 Nginx 来显示包含随机生成的文本的网页。 创建一个 index.html 文件并将以下代码复制并粘贴到该文件中。

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

要生成此文本,请安装 Lorem 插件并在您的开发环境中按代码 > 生成 > 生成文本以生成包含 10 个句子的文本。


定义镜像

创建一个名为 Dockerfile 的文件,然后将以下说明复制并粘贴到该文件中。

FROM nginx:1.23.1-alpine
COPY . /usr/share/nginx/html
  1. FROM - 这定义了使用后续指令在其上创建自定义图像的基础图像。
  2. COPY - 这会将当前目录中的所有文件和目录复制到图像文件系统目标 /usr/share/nginx/html。

要了解有关使用 Nginx 映像的更多信息,Docker hub Nginx 官方映像提供了有关创建映像和从中运行容器的详细信息。


构建 Nginx 镜像

要构建我们应用程序的 Nginx 映像,请使用以下命令创建一个带有标签 host-ip:latest 的映像。

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

此命令顺序执行 Dockerfile 的指令。 可以从终端窗口查看该过程,如下所示。

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

运行容器

在本节中,我们希望从正在运行的容器中查看主机的 IP 地址。 由于我们没有正在运行的容器,我们首先需要使用以下命令运行一个容器。

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

此命令运行一个名为 host-ip-service 的容器,该容器在主机上公开端口 8000 以侦听容器上的 post 80。 要查看应用程序的内容,请在浏览器上向 localhost:8000 (http://localhost:8000/) 发出请求,将显示一个包含随机文本的网页。


从 Docker 容器内部获取 Docker 主机的 IP 地址

由于我们想在正在运行的容器中运行命令,因此使用以下命令 bash 到 Docker 容器中。

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

输出:

/ #

要获取运行我们的容器 host-ip-service 的 docker 主机的 IP 地址,请键入以下命令并按键盘上的 Enter 按钮。

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

输出:

172.17.0.1

请注意 ,由于我们使用的是 Docker 网桥,这是容器的默认设置,我们将获得网桥的 IP 地址,即 172.17.0.1,而不是主机连接的网络上的 IP 地址。


总结

在本文中,我们学习了利用 Nginx 应用程序从容器内部获取 Docker 主机的 IP 地址。 请注意,根据容器运行的位置,还有其他方法可以实现相同的目标,我们应该自由使用满足要求的任何方法。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Solution to incorrect access log time when deploying Nginx in Docker

发布时间:2025/03/26 浏览次数:165 分类:Docker

In the process of operating the website, I never took the logs too seriously. Although logging was turned on, I never analyzed the logs carefully. Today, when I looked at the logs on a whim, I found that the recorded time was 8 hours less t

Docker deploys nginx php application

发布时间:2025/03/26 浏览次数:131 分类: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

发布时间:2025/03/26 浏览次数:107 分类: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

发布时间:2025/03/26 浏览次数:97 分类: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

发布时间:2025/03/26 浏览次数:124 分类: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

发布时间:2025/03/26 浏览次数:202 分类: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

发布时间:2025/03/26 浏览次数:87 分类: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

发布时间:2025/03/26 浏览次数:140 分类: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

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便