创建新容器后,是否可以运行命令从主机获取容器的IP地址?
基本上,一旦Docker创建了容器,我就想滚动我自己的代码部署和容器配置脚本。
创建新容器后,是否可以运行命令从主机获取容器的IP地址?
基本上,一旦Docker创建了容器,我就想滚动我自己的代码部署和容器配置脚本。
当前回答
没有人提及的最佳方法是为容器分配主机名。
docker run -d --hostname localcontainerhostname imageName
这将为您提供ip地址,但您可能还是希望使用主机名
nslookup localcontainerhostname
其他回答
在Docker 1.3+中,您也可以使用以下方法进行检查:
输入正在运行的Docker(Linux):
docker exec [container-id or container-name] cat /etc/hosts
172.17.0.26 d8bc98fa4088
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.17 mysql
对于窗口:
docker exec [container-id or container-name] ipconfig
这是我今天用Python开发的一个解决方案,使用docker inspect容器JSON输出作为数据源。
我有很多容器和基础设施需要检查,我需要从任何容器中以快速、漂亮的方式获取基本的网络信息。这就是我写这个剧本的原因。
重要提示:从1.9版开始,Docker允许您创建多个网络并将它们连接到容器。
#!/usr/bin/python
import json
import subprocess
import sys
try:
CONTAINER = sys.argv[1]
except Exception as e:
print "\n\tSpecify the container name, please."
print "\t\tEx.: script.py my_container\n"
sys.exit(1)
# Inspecting container via Subprocess
proc = subprocess.Popen(["docker","inspect",CONTAINER],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
out = proc.stdout.read()
json_data = json.loads(out)[0]
net_dict = {}
for network in json_data["NetworkSettings"]["Networks"].keys():
net_dict['mac_addr'] = json_data["NetworkSettings"]["Networks"][network]["MacAddress"]
net_dict['ipv4_addr'] = json_data["NetworkSettings"]["Networks"][network]["IPAddress"]
net_dict['ipv4_net'] = json_data["NetworkSettings"]["Networks"][network]["IPPrefixLen"]
net_dict['ipv4_gtw'] = json_data["NetworkSettings"]["Networks"][network]["Gateway"]
net_dict['ipv6_addr'] = json_data["NetworkSettings"]["Networks"][network]["GlobalIPv6Address"]
net_dict['ipv6_net'] = json_data["NetworkSettings"]["Networks"][network]["GlobalIPv6PrefixLen"]
net_dict['ipv6_gtw'] = json_data["NetworkSettings"]["Networks"][network]["IPv6Gateway"]
for item in net_dict:
if net_dict[item] == "" or net_dict[item] == 0:
net_dict[item] = "null"
print "\n[%s]" % network
print "\n{}{:>13} {:>14}".format(net_dict['mac_addr'],"IP/NETWORK","GATEWAY")
print "--------------------------------------------"
print "IPv4 settings:{:>16}/{:<5} {}".format(net_dict['ipv4_addr'],net_dict['ipv4_net'],net_dict['ipv4_gtw'])
print "IPv6 settings:{:>16}/{:<5} {}".format(net_dict['ipv6_addr'],net_dict['ipv6_net'],net_dict['ipv6_gtw'])
输出为:
$ python docker_netinfo.py debian1
[frontend]
02:42:ac:12:00:02 IP/NETWORK GATEWAY
--------------------------------------------
IPv4 settings: 172.18.0.2/16 172.18.0.1
IPv6 settings: null/null null
[backend]
02:42:ac:13:00:02 IP/NETWORK GATEWAY
--------------------------------------------
IPv4 settings: 172.19.0.2/16 172.19.0.1
IPv6 settings: null/null null
Linux容器
.方法1
docker exec postgres ifconfig
.方法2
docker exec postgres cat /etc/hosts
.方法3
码头管理人员
.方法4
使用Powershell
docker inspect postgres | select-string 'ipaddress'
如果您需要一个特定的方便别名来获取特定的容器ip,请使用此别名,并提供公认的答案
alias dockerip='f(){ docker inspect $1|grep -i "ipaddress.*[12]*\.[0-9]*"|sed -e "s/^ *//g" -e "s/[\",]//g" -e "s/[*,]//g" -e "s/[a-zA-Z: ]//g" | sort --unique; unset -f f; }; f'
然后你可以用
dockerip <containername>
您也可以使用contained而不是containername
BTW接受了很好的答案,但没有产生干净的输出,所以我编辑了它,并这样使用;
alias dockerips='for NAME in $(docker ps --format {{.Names}}); do echo -n "$NAME:"; docker inspect $NAME|grep -i "ipaddress.*[12]*\.[0-9]*"|sed -e "s/^ *//g" -e "s/[\",]//g" -e "s/[_=*,]//g" -e "s/[a-zA-Z: ]//g "| sort --unique;done'
在Windows PowerShell中尝试:
docker inspect -f "{{ .NetworkSettings.Networks.nat.IPAddress }}" <container id>