5.31.2014

Docker: Python: Getting Internal IP Address of the Container

コンテナを Detached Mode で起動して、その内部アドレスを知る一行野郎 (ワンライナー)

grep や jq でもできるけど、Python で JSON をパーズするほうが好み。

docker inspect $(docker run -d IMAGE_NAME) | \
python -c 'import json,sys;print json.loads(sys.stdin.read())[0]["NetworkSettings"]["IPAddress"]'

 

実行例

Docker の動作するホスト上で実行

# docker inspect $(docker run -d mogproject/sshd) | python -c 'import json,sys;print json.loads(sys.stdin.read())[0]["NetworkSettings"]["IPAddress"]'
Unable to find image 'mogproject/sshd' locally
Pulling repository mogproject/sshd
db38b1b40c65: Download complete
539c0211cd76: Download complete
154a1496c839: Download complete
0e8300b2772a: Download complete
43af4236d0da: Download complete
fc0cc09b2562: Download complete
172.17.0.2
# ssh ssh-user@172.17.0.2
The authenticity of host '172.17.0.2 (172.17.0.2)' can't be established.
RSA key fingerprint is e7:77:e5:bc:b0:3e:c4:78:27:cf:57:3a:2e:98:69:30.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.17.0.2' (RSA) to the list of known hosts.
ssh-user@172.17.0.2's password:
[ssh-user@1e53a210ba41 ~]$ cat /etc/redhat-release
CentOS release 6.4 (Final)

0 件のコメント:

コメントを投稿