如何配置我的本地机器以使用docker托管的dns服务器以及特定域的其他dns设置?

如何配置我的本地机器以使用docker托管的dns服务器以及特定域的其他dns设置?

我有以下docker-compose.yml用作php web应用程序本地解决方案:

version: "3.1"

services:
  nginx:
    image: nginx:1.13
    volumes:
      - "./configuration/nginx.conf:/etc/nginx/nginx.conf:ro"
      - "./configuration/etable-local.key:/etc/nginx/etable-local.key:ro"
      - "./configuration/etable-local.crt:/etc/nginx/etable-local.crt:ro"
      - "website:/var/www/html/website"
      - "api:/var/www/html/static"
    links:
      - "php708:website"
      - "php72:api"
    networks:
      frontend:
        ipv4_address: 172.20.0.5
    ports:
      - "80:80"
      - "443:443"

  php72:
    image: php:7.2-fpm-alpine
    volumes:
      - "api:/var/www/html/api"
    environment:
      ENVIRONMENT: local
    networks:
      - frontend
    dns:
      - 8.8.8.8
      - 9.9.9.9
  website:
    image: php:7.2-fpm-alpine
    volumes:
      - "api:/var/www/html/api"
    environment:
      ENVIRONMENT: local
    networks:
      - frontend
    dns:
      - 8.8.8.8
      - 9.9.9.9
volumes:

  website:
    driver: local
    driver_opts:
      type: "none"
      o: "bind"
      device: $WEBSITE_DIR

  api:
    driver: local
    driver_opts:
      type: "none"
      o: "bind"
      device: $API_DIR

networks:
  frontend:
    ipam:
      config:
        - subnet: 172.20.0.0/24

在运行 docker 容器的计算机中,/etc/hosts 中有以下条目:

172.20.0.5 api.local
172.20.0.5 website.local

但我也想提供通配符域名,因此我考虑在我的 docker-compose 中也附带一个 dnsmasq 并进行适当的配置,然后完全放弃/etc/hosts(更好的可重用性和跨团队的共享设置):

version: "3.1"

services:
  nginx:
    image: nginx:1.13
    volumes:
      - "./configuration/nginx.conf:/etc/nginx/nginx.conf:ro"
      - "./configuration/etable-local.key:/etc/nginx/etable-local.key:ro"
      - "./configuration/etable-local.crt:/etc/nginx/etable-local.crt:ro"
      - "website:/var/www/html/website"
      - "api:/var/www/html/static"
    links:
      - "php708:website"
      - "php72:api"
    networks:
      frontend:
        ipv4_address: 172.20.0.5
    ports:
      - "80:80"
      - "443:443"

  php72:
    image: php:7.2-fpm-alpine
    volumes:
      - "api:/var/www/html/api"
    environment:
      ENVIRONMENT: local
    networks:
      - frontend
    dns:
      - 8.8.8.8
      - 9.9.9.9

  website:
    image: php:7.2-fpm-alpine
    volumes:
      - "api:/var/www/html/api"
    environment:
      ENVIRONMENT: local
    networks:
      - frontend
    dns:
      - 8.8.8.8
      - 9.9.9.9

  dnsmasq:
     image: 'jpillora/dnsmasq'
     ports:
      - "53:53/udp" 
      - "5380:8080"
    volumes:
       -"/opt/dnsmasq.conf:/etc/dnsmasq.conf"
    environment:
        HTTP_USER: foo
        HTTP_PASS: bar
    networks:
      frontend:
        ipv4_address: 172.20.0.6
    dns:
      - 8.8.8.8
      - 9.9.9.9

volumes:

  website:
    driver: local
    driver_opts:
      type: "none"
      o: "bind"
      device: $WEBSITE_DIR

  api:
    driver: local
    driver_opts:
      type: "none"
      o: "bind"
      device: $API_DIR

networks:
  frontend:
    ipam:
      config:
        - subnet: 172.20.0.0/24

但是我如何配置我的本地 GNU/Linux 机器和 Windows 来通过172.20.0.6任何其他域来解析适当的域?

在我的 linux mint 19.04 中,一旦运行 ifconfig,我就会得到以下设置:

br-144546484da6: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.18.0.1  netmask 255.255.0.0  broadcast 172.18.255.255
        ether 02:42:ee:ad:b4:6b  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

br-2c821d16ac40: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.24.0.1  netmask 255.255.0.0  broadcast 172.24.255.255
        ether 02:42:d1:29:8f:d0  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

br-936b043d07ec: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.20.0.1  netmask 255.255.255.0  broadcast 172.20.0.255
        inet6 fe80::42:31ff:fe3b:6a93  prefixlen 64  scopeid 0x20<link>
        ether 02:42:31:3b:6a:93  txqueuelen 0  (Ethernet)
        RX packets 21190  bytes 46944301 (46.9 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 20152  bytes 3393362 (3.3 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:63:fa:1e:98  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp3s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.2.4  netmask 255.255.255.0  broadcast 192.168.2.255
        inet6 fe80::4819:e008:32b3:eb75  prefixlen 64  scopeid 0x20<link>
        ether 30:5a:3a:82:3c:2c  txqueuelen 1000  (Ethernet)
        RX packets 1020236  bytes 974038110 (974.0 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 708231  bytes 83763973 (83.7 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 35856  bytes 3871878 (3.8 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 35856  bytes 3871878 (3.8 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vboxnet3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.10.1  netmask 255.255.255.0  broadcast 192.168.10.255
        inet6 fe80::800:27ff:fe00:3  prefixlen 64  scopeid 0x20<link>
        ether 0a:00:27:00:00:03  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1537  bytes 248272 (248.2 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

veth15937a1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::f828:5eff:fe17:5f3f  prefixlen 64  scopeid 0x20<link>
        ether fa:28:5e:17:5f:3f  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1299  bytes 200892 (200.8 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

veth4f39177: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::7807:8bff:fef4:90d6  prefixlen 64  scopeid 0x20<link>
        ether 7a:07:8b:f4:90:d6  txqueuelen 0  (Ethernet)
        RX packets 4022  bytes 2287668 (2.2 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4674  bytes 1238082 (1.2 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vethb2863e3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::986d:2aff:feae:5a83  prefixlen 64  scopeid 0x20<link>
        ether 9a:6d:2a:ae:5a:83  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1279  bytes 197402 (197.4 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vethe23b637: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::d8cf:8dff:fe79:2de  prefixlen 64  scopeid 0x20<link>
        ether da:cf:8d:79:02:de  txqueuelen 0  (Ethernet)
        RX packets 10344  bytes 1065309 (1.0 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 11282  bytes 1252992 (1.2 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vethea02dde: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::a8aa:72ff:fe3f:f7f6  prefixlen 64  scopeid 0x20<link>
        ether aa:aa:72:3f:f7:f6  txqueuelen 0  (Ethernet)
        RX packets 1792  bytes 43279416 (43.2 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2443  bytes 307732 (307.7 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vethf1bdbae: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::6cd3:dfff:feec:1000  prefixlen 64  scopeid 0x20<link>
        ether 6e:d3:df:ec:10:00  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1272  bytes 196493 (196.4 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vethf209ff2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::7c6c:11ff:fecc:25c7  prefixlen 64  scopeid 0x20<link>
        ether 7e:6c:11:cc:25:c7  txqueuelen 0  (Ethernet)
        RX packets 5594  bytes 45271086 (45.2 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 7041  bytes 45379695 (45.3 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

并且 dns 运行的网络是:br-936b043d07ec但我担心每次运行时它的名称也可能会改变docker-compose up(一旦我完成我的工作我就会经常运行docker-compose down)。

很多解决方案都类似这样的建议 DNS 解析里面docker 容器。这不是我想要的,而是一种以类似的方式进行通配符名称解析的方法,我可以在中设置本地域/etc/hosts

相关内容