简体   繁体   中英

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.

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:

$ 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.

How docker-compose specifically set up this container such that curl knows the IP address?

Docker provides a DNS service (search for “automatic DNS resolution between containers”), and Docker Compose provides a default “user-defined” network that uses it . 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.

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. You do not need to set container_name: , hostname: , links: , or networks: ; this is all configured automatically for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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