从主机上使用 docker inspect 获取 Docker 容器的 IP 地址
Docker 容器不仅仅用于隔离——它们通常用于管理仍需要直接相互通信的进程。 但是,要进行通信,您通常需要知道每个容器的 IP 地址,您可以通过一些命令找到该地址。
考虑使用用户定义的 bridge
Docker 网络有点复杂。默认启动的容器将被放置在默认的“桥接网络”中,并允许直接与其他容器通信,前提是您拥有它们的私有 IP 地址。可以关闭此选项以实现真正的隔离,但默认情况下不是这样。
如果不想绑定端口,也可以使用此地址与主机操作系统进行通信。这是直接通过容器的 IP 地址访问容器的主要用例,但你可能仍然应该只绑定一个端口(可以在防火墙中将其与 Internet 隔绝)。
但是,IP 地址是临时的,并且在容器停止和启动时很容易中断。对于容器之间的通信,Docker 通过用户定义的桥接网络提供了一个解决方案,如果你有多个容器相互通信,那么可能应该需要使用它。
添加到非默认网络的容器将能够通过它们的别名相互访问,这将自动解析为私有 IP。你可以创建新网络,在这些网络中运行容器,并将现有容器连接到网络。然后,可以使用别名作为主机名访问其他容器;比如这里的NGINX容器可以通过连接字符串mongodb://mongohost:27017
访问 MongoDB 实例。
$ docker network create example
$ docker run --net example --name nginx -d nginx
$ docker network connect example --alias mongohost mongodb
使用网桥有很多好处,建议使用传统的 --link
选项,它适用于默认网络。 主要问题是用户定义网络中的容器将暴露给彼此的端口,无论它们是否发布,但你可以设置多个网络,因此这通常不是问题。
另一个缺点是,由于用户定义的网络提供了更好的隔离,它们也不允许你使用其私有 IP 地址跨网络访问容器。 默认网络中的所有容器都可以相互通信,但是一旦将其删除并放置在用户定义的网络中,该功能就会被禁用。 但是,我们也可以只在默认和用户定义的网络中启动容器,因此如果你选择使容器对其他人可见,这也不是问题。
从 Docker 获取 IP 地址
如果你只想要 IP 地址,那么从主机操作系统获取则非常简单。 首先,需要找到要获取其信息的容器的 ID 或名称,可以使用下面的命令来获取容器:
$ docker ps
然后,运行 docker inspect
,它会返回一个巨大的 JSON 文件,其中包含有关容器的所有信息。 不过,我们只对 IP 地址感兴趣,因此我们可以使用 -f
选项向它传递一个格式化选项,以将其缩小到仅包含IP地址。
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' name_or_id
该命令运行良好,但它也仅仅是返回你指定ID容器的信息。 如果你想要一个更具体的解决方案,可以使用 docker network inspect
打印给定网络中所有容器的信息,可以选择格式化为 JSON 查找表:
docker network inspect bridge -f '{{json .Containers}}'
输出结果如下
{
"2321e218d0880e8520de254b4bd2773ccf67c5d7e00fe1fc5a53a8bd1113a48d": {
"Name": "cranky_snyder",
"EndpointID": "2e8210889328695ba2a07262993ec8550ea2dd7e592b008ede989e6cac943372",
"MacAddress": "02:42:ac:11:00:02",
"IPv4Address": "172.17.0.2/16",
"IPv6Address": ""
}
}
获取容器的网络配置
Docker 容器实际上只是一种隔离机制,我们可以像登录普通主机一样进入容器并运行 ifconfig
等常规 Linux 命令并以这种方式获取 IP 地址。
为此,我们需要使用 docker ps
获取容器名称或 ID,然后运行 exec -it
。在本例中,打印出所有 IP 信息:
docker exec -it 2321e218d088 ip a
相关文章
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