获取 Docker 容器的 IP 地址
本文演示如何获取 Docker 容器 IP 地址的 IP 地址。
连接到 Bridge 网络并获取 Docker 容器的 IP 地址
docker 容器如此方便的重要原因之一是我们可以轻松地连接它们。这允许容器通信并轻松共享资源。
我们还可以将容器连接到无 docker 工作负载。
这篇文章不是关于 docker 网络的;因此,我们不会深入研究 docker 网络的细节。但是,docker 提供了各种驱动程序,使用桥驱动程序使网络可插入。
Docker 允许我们使用其默认驱动程序(称为桥接驱动程序)创建网络。但是,桥接网络是私有的,其范围仅限于主机上的容器。
你可以查看主机上的默认网络以及你之前拥有的网络。每个 docker 安装通常包括如下所示的三个默认网络。
isaac@DESKTOP-HV44HT6:~$ docker network ls
NETWORK ID NAME DRIVER SCOPE
9729860fa596 bridge bridge local
0667c3f7d0f0 host host local
c3273b158256 none null local
除非另有说明,否则 Docker 将始终在桥接网络中启动新容器。
容器 IP 地址是联网 docker 容器的重要组成部分。通常,容器会为它们连接的每个网络分配一个 IP 地址。
另一方面,你也可以使用以下命令手动将 docker 容器连接到桥接网络。
$ docker run -dt rabbitmq
这将创建一个 docker 容器并将其分配给桥接网络。在我们可以检查网络以确认它是否已连接到它之前,我们应该首先确保容器正在运行。
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
42487cad0390 rabbitmq "docker-entrypoint.s…" About a minute ago Up About a minute 4369/tcp, 5671-5672/tcp, 15691-15692/tcp, 25672/tcp compassionate_keller
检查桥接网络将允许我们查看连接到网络的容器,以及其他详细信息,例如容器的 IP 地址和容器的默认子网。详细信息以 JSON 格式返回,如下所示。
~$ docker network inspect bridge
输出:
"ConfigOnly": false,
"Containers": {
"42487cad0390a8de6d1a88bc1d6c09ffdf3162dc85d4d5d3dc70200b2348b673": {
"Name": "compassionate_keller",
"EndpointID": "ffa55232565d3a32d1e471d0753c1f491d6131a5cca50b8b76bc43a8ff554e32",
"MacAddress": "02:42:ac:11:00:04",
"IPv4Address": "172.17.0.4/16",
"IPv6Address": ""
}
上例中我们容器的 IP 地址是 172.17.0.4/16。
我们也可以有多个容器连接到网络,我们可以通过检查桥接网络轻松找到两者的 IP 地址。如下图,我们在 bridge 网络中添加一个基于 Nginx 镜像的容器。
isaac@DESKTOP-HV44HT6:~$ docker run -dt nginx
4ab752ab92582a0eb2cb14475094460fc8cc608c93a357a8dca082cfea2bc368
现在,如果我们检查桥接网络,我们将能够获得两个容器的 IP 地址。请记住,这两个容器应该正在运行。
isaac@DESKTOP-HV44HT6:~$ docker network inspect bridge
输出:
"Containers": {
"42487cad0390a8de6d1a88bc1d6c09ffdf3162dc85d4d5d3dc70200b2348b673": {
"Name": "compassionate_keller",
"EndpointID": "ffa55232565d3a32d1e471d0753c1f491d6131a5cca50b8b76bc43a8ff554e32",
"MacAddress": "02:42:ac:11:00:04",
"IPv4Address": "172.17.0.4/16",
"IPv6Address": ""
},
"4ab752ab92582a0eb2cb14475094460fc8cc608c93a357a8dca082cfea2bc368": {
"Name": "trusting_keller",
"EndpointID": "a2babd11586f493cf5e57f5d9920a7f5648cf6163e77030521bef62fc9f34a63",
"MacAddress": "02:42:ac:11:00:02",
"IPv4Address": "172.17.0.2/16",
"IPv6Address": ""
}
}
两个容器的 IP 地址按创建顺序分别为 172.17.0.4/16
和 172.17.0.2/16
。
使用 docker inspect
获取 Docker 容器的 IP 地址
我们还可以使用 docker inspect
和容器名称或容器 id 来查找特定 Docker 容器的 IP 地址。这同样会以 JSON 格式返回许多其他详细信息。
$ docker inspect 42487cad0390
输出:
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "9729860fa5961eeac55f921ee787b2c82a15146cd36117b5394243be2149e929",
"EndpointID": "ffa55232565d3a32d1e471d0753c1f491d6131a5cca50b8b76bc43a8ff554e32",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.4",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:04",
"DriverOpts": null
}
}
在这种情况下,我们已经根据官方 Nginx 镜像检查了容器。你可以参考 Docker 文档,了解有关联网 Docker 容器的有趣内容。
相关文章
Get the IP address of the Docker container from the host using docker inspect
发布时间:2025/03/26 浏览次数:102 分类:Docker
-
Docker containers are not just for isolation—they are often used to manage processes that still need to communicate directly with each other. However, to communicate, you usually need to know the IP address of each container, which you ca
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