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

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

问题描述

我正在使用Mac的Docker.我在Docker容器中运行基于Node.js的微服务.我想通过浏览器测试节点微服务.如何获取正在运行的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?

推荐答案

如果您不想将端口从主机映射到容器,则可以直接访问容器的docker range ip.默认情况下,仅从主机访问该范围.您可以执行以下操作来检查您的容器网络数据:

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>

可能更好地进行过滤:

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

通常,默认docker ip范围是172.17.0.0/16.如果一切正常,并且您未指定任何特殊的网络选项,则主机应为172.17.0.1,而第一个容器应为172.17.0.2.

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.

编辑 使用docker功能而不是"bash欺骗"的另一种更优雅的方法:

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

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

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

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