简体   繁体   English

docker-compose如何将我的服务名称映射到IP地址?

[英]how does docker-compose map my service names into IP addresses?

I have a docker-compose.yml file that I use to run a web application. 我有一个用于运行Web应用程序的docker-compose.yml文件。

version: '3'
services:
  web:
    entrypoint: gunicorn myapp.wsgi:application --bind 0.0.0.0:7000
    ...
  nginx:
    ports:
      # HOST:CONTAINER
      - 80:80
      - 443:443
    ...

Inside my nginx service's container: 在我的nginx服务的容器中:

$ cat /etc/hosts

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
192.168.144.4   790c0822b887

$ curl http://web:7000

<h1>Bad Request (400)</h1>

$ traceroute web

traceroute to web (192.168.144.3), 30 hops max, 46 byte packets
 1  ec2-user_web_1.ec2-user_default (192.168.144.3)  0.008 ms  0.007 ms  0.006 ms

So I can lookup the web hostname and it's mapped to the IP address 192.168.144.3 , but I don't see it in the /etc/hosts file. 因此,我可以查找Web主机名并将其映射到IP地址192.168.144.3 ,但是在/etc/hosts文件中看不到它。

How docker-compose specifically set up this container such that curl knows the IP address? docker-compose如何专门设置此容器,以便curl知道IP地址?

Docker provides a DNS service (search for “automatic DNS resolution between containers”), and Docker Compose provides a default “user-defined” network that uses it . Docker提供了DNS服务 (搜索“容器之间的自动DNS解析”),而Docker Compose提供了使用它的默认“用户定义”网络 If you look at /etc/resolv.conf inside your container, it should point at a Docker-private IP addresses which provides hostname resolution for both the general Internet and for other containers. 如果查看容器中的/etc/resolv.conf ,它应该指向一个Docker私有IP地址,该IP地址为通用Internet和其他容器提供主机名解析。

More specifically in a Docker Compose context, the name of each entry in the services: block is resolvable as a host name by other containers launched from the same docker-compose.yml file. 更具体地说,在Docker Compose上下文中, services:块中每个条目的名称可由从同一docker-compose.yml文件启动的其他容器作为主机名解析。 You do not need to set container_name: , hostname: , links: , or networks: ; 您无需设置container_name:hostname:links:networks: this is all configured automatically for you. 这都是为您自动配置的。

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

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