如何获取正在运行的 docker 容器的 IP 地址 [英] How to get IP address of running docker container

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

问题描述

I am using Docker for Mac. I am running a nodejs based microservice in a Docker container. I want to test node microservice through the browser. How to get IP address of running docker container?

解决方案

If you don't want to map ports from your host to the container you can access directly to the docker range ip for the container. This range is by default only accessed from your host. You can check your container network data doing:

docker inspect <containerNameOrId>

Probably is better to filter:

docker inspect <containerNameOrId> | grep '"IPAddress"' | head -n 1

Usually, the default docker ip range is 172.17.0.0/16. Your host should be 172.17.0.1 and your first container should be 172.17.0.2 if everything is normal and you didn't specify any special network options.

EDIT Another more elegant way using docker features instead of "bash tricking":

docker inspect -f "{{ .NetworkSettings.IPAddress }}" <containerNameOrId>

EDIT2 For modern docker engines, now it is this way (thanks to the commenters!):

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <containerNameOrId>

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

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