简体   繁体   English

如何从容器内部访问正在运行的docker容器外部ip?

[英]How to access a running docker containers external ip from within the container?

When I deploy via docker-compose , I can run inspect on any of the containers and see the external ip. 当我通过docker-compose部署时,我可以在任何容器上运行inspect并查看外部ip。

$ docker inspect --format '{{ .NetworkSettings.IPAddress }}' parentserver
>>>172.17.0.3

My question is, if someone were to, say, git clone my repository on their local machine and run docker-compose , is it guaranteed that the IP of parentserver would be 172.17.0.3 on their machine as well? 我的问题是,如果有人要在自己的本地计算机上git clone我的存储库并运行docker-compose ,是否可以确保parentserver的IP在其计算机上也为172.17.0.3?

The reason I ask is I have microservices that talk to each other via HTTP, and I currently have the IPs hardcoded inside my apps logic. 我问的原因是我拥有通过HTTP相互通信的微服务,而我目前在我的应用程序逻辑中已对IP进行了硬编码。

If the IP of each docker container is guaranteed to be the same no matter where it's run, this wouldn't be an issue, either way I need to find a better solution which is why I'm wondering if I can query the external IP from running docker containers from within the container? 如果确保每个docker容器的IP无论在哪里运行都相同,那么这不是问题,无论哪种方式,我都需要找到更好的解决方案,这就是为什么我想知道是否可以查询外部IP从容器内部运行docker容器?

Docker containers created via docker-compose are put in a shared network by default and are hooked to an embedded DNS server. 通过docker-compose创建的Docker容器默认情况下放置在共享网络中,并钩接到嵌入式DNS服务器上。 This means they are linked by default. 这意味着它们是默认链接的。 A container "A" linked to container "B" can ping container "B" by its assigned link name, by default, the container name. 链接到容器“ B”的容器“ A”可以通过为其分配的链接名称(默认情况下是容器名称)来ping容器“ B”。

Take as an example my nginx config: 以我的nginx配置为例:

version: '2'

services:
  # ... more config

  nginx:
    environment:
      API_DOMAIN:     api.mysite.com
      FRONT_DOMAIN:   'mysite.com www.mysite.com'
      ERRBIT_DOMAIN:  errbit.mysite.com
      ERRBIT_HOST:    http://errbit:8080
      PGADMIN_DOMAIN: pgadmin.mysite.com
      PGADMIN_HOST:   http://pgadmin:5050

  errbit:
    env_file: ./production/errbit.env

I've made an nginx image that accepts domains via environment vars, and the hosts are the names of other containers running in the same docker host created using this config. 我已经制作了一个nginx映像,该映像通过环境vars接受域,并且主机是在使用此配置创建的同一docker主机中运行的其他容器的名称。

https://docs.docker.com/engine/userguide/networking/ https://docs.docker.com/engine/userguide/networking/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM