如何从主机获取 Docker 容器的 IP 地址 [英] How to get a Docker container's IP address from the host

查看:42
本文介绍了如何从主机获取 Docker 容器的 IP 地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there a command I can run to get the container's IP address right from the host after a new container is created?

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

Basically, once Docker creates the container, I want to roll my own code deployment and container configuration scripts.

推荐答案

inspect--format 选项来救援.

现代 Docker 客户端语法是:

Modern Docker client syntax is:

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

旧的 Docker 客户端语法是:

Old Docker client syntax is:

docker inspect --format '{{ .NetworkSettings.IPAddress }}' container_name_or_id

这些命令将返回 Docker 容器的 IP 地址.

These commands will return the Docker container's IP address.

如评论中所述:如果您使用的是 Windows,请在大括号周围使用双引号 " 而不是单引号 '.

As mentioned in the comments: if you are on Windows, use double quotes " instead of single quotes ' around the curly braces.

这篇关于如何从主机获取 Docker 容器的 IP 地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆