创建新容器后,是否可以运行命令从主机获取容器的IP地址?

基本上,一旦Docker创建了容器,我就想滚动我自己的代码部署和容器配置脚本。


当前回答

使用Python新API:

import docker

client = docker.DockerClient()
container = client.containers.get("NAME")
ip_add = container.attrs['NetworkSettings']['IPAddress']
print(ip_add)

其他回答

docker inspect <container id> | grep -i ip

例如:

docker inspect 2b0c4b617a8c | grep -i ip

Docker是用Go编写的,它也使用Go语法进行查询。

要检查特定容器的IP地址,需要运行命令(-f表示“format”):

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_id_or_name

对于容器ID或名称,可以运行以下命令

docker container ls

它将列出每个正在运行的容器。

Linux容器

.方法1

 docker exec postgres ifconfig

.方法2

  docker exec postgres cat /etc/hosts

.方法3

码头管理人员

.方法4

使用Powershell

docker inspect postgres | select-string 'ipaddress'

Use:

docker inspect $CID | grep IPAddress | grep -v null| cut -d '"' -f 4 | head -1

首先获取容器ID:

docker ps

(第一列用于容器ID)

使用容器ID运行:

docker inspect <container ID>

在底部的“网络设置”下,您可以找到IPAddress

或者只针对基于UNIX的系统:

docker inspect <container id> | grep "IPAddress"

对于Windows CMD:

docker inspect <container id> | findstr "IPAddress"