简体   繁体   English

如何使用 Windows 的 Docker 为 Windows 容器配置 DNS?

[英]How to configure DNS for Windows containers using Docker for Windows?

I'm running Windows containers on Docker for Windows.我在 Docker 上为 Windows 运行 Windows 容器。 My docker-compose.yml looks like this:我的docker-compose.yml看起来像这样:

version: '2.4'
services:
  web:
    image: microsoft/aspnet
    platform: windows
    networks:
      webnet:
        ipv4_address: 172.0.0.2
    ports:
      - "4119:80"
    expose:
      - "80"
    dns:
      - 8.8.8.8
      - 8.8.4.4

networks:
  webnet:
    name: "custom_network"
    ipam:
      config:
        - subnet: 172.0.0.0/16
          gateway: 172.0.0.1

When I attach PowerShell and run ping 8.8.8.8 the ping goes fine.当我附加 PowerShell 并运行ping 8.8.8.8时,ping 正常。 But when I run ping a domain name I have the following result:但是当我运行 ping 域名时,我得到以下结果:

PS C:\> ping google.com
Ping request could not find host google.com. Please check the name and try again.

And it's the same for any other IPs/domains.对于任何其他 IP/域也是如此。 So my container has an access to Internet, but it cannot resolve domain names.所以我的容器可以访问 Internet,但它无法解析域名。 As a workaround, I've tried to inject pre filled hosts file into container and then ping by domains from that file works fine.作为一种解决方法,我尝试将预填充的hosts文件注入容器,然后通过该文件中的域 ping 正常工作。 It resolves IP address correctly and receives replies from remote machine.它正确解析 IP 地址并接收来自远程机器的回复。

What did I miss?我错过了什么? How should I configure Docker containers to solve this issue?我应该如何配置 Docker 容器来解决这个问题?

Update: I have the following Docker version on my PC:更新:我的电脑上有以下 Docker 版本:

> docker version
Client: Docker Engine - Community
 Version:           19.03.2
 API version:       1.40
 Go version:        go1.12.8
 Git commit:        6a30dfc
 Built:             Thu Aug 29 05:26:49 2019
 OS/Arch:           windows/amd64
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          19.03.2
  API version:      1.40 (minimum version 1.24)
  Go version:       go1.12.8
  Git commit:       6a30dfc
  Built:            Thu Aug 29 05:39:49 2019
  OS/Arch:          windows/amd64
  Experimental:     true
> docker-compose version
docker-compose version 1.24.1, build 4667896b
docker-py version: 3.7.3
CPython version: 3.6.8
OpenSSL version: OpenSSL 1.0.2q  20 Nov 2018

Per Here you should be able to simply set: -o com.docker.network.windowsshim.dnsservers=<DNS SERVER/S> when creating the docker network if doing it from the CLI. Per Here您应该能够简单地设置: -o com.docker.network.windowsshim.dnsservers=<DNS SERVER/S>如果从 CLI 执行它,则在创建 docker 网络时。

If doing it from a docker-compose file see this answer which recommends如果从 docker-compose 文件执行此操作,请参阅建议的此答案

  dns:
   - 8.8.8.8
   - 4.4.4.4
   - 192.168.9.45

Or setting it in the VM itself for windows.或者在 VM 本身中为 windows 设置它。

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

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