Connection error to external sites from a Docker container on Windows Server
I cannot connect to internet sites from a Docker container on Windows Server. The container runs a command to download and execute a script, but it fails due to a DNS error.
Example Dockerfile:
FROM windows/servercoreSHELL ["Powershell"]
ENV UseCompression false; RUN iex ((new-object net.webclient).DownloadString('https://example.com/install.ps1'));
When running this, I get the following error:
Exception calling "DownloadString" with "1" argument(s): "The remote name could not be resolved: 'example.com'"
The container cannot access external DNS servers.
How can I fix this?
Answers
Anna Schmidt
Alternatively, try resetting the network settings by switching the container's network adapter from NAT to bridge mode and back. This can sometimes resolve connectivity issues.
José Sousa
You can add DNS servers directly in the daemon.json file, for example:
Check the DNS on the host by running:
After saving changes in daemon.json, restart Docker and test access to external sites.
Daniel Karlsson
The issue might be that the Docker container is using the DNS server configured for the NAT network interface. Try adding the --dns parameter with the DNS server's IP to specify which DNS to use for Docker.
Example:
Or add a DNS entry in the daemon.json file:
Restart Docker to apply the changes.