The Error MessageYou are halfway through a docker build when the process suddenly grinds to a halt. This usually happens at the RUN apt-get update step. Instead of downloading packages, your console spits out a wall of red text:
Err:1 http://archive.ubuntu.com/ubuntu jammy InRelease
Temporary failure resolving 'archive.ubuntu.com'
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/jammy/InRelease Temporary failure resolving 'archive.ubuntu.com'
W: Some index files failed to download. They have been ignored, or old ones used instead.
Why DNS Fails in DockerDocker containers usually inherit DNS settings from your host machine. However, modern Linux distributions like Ubuntu 22.04 use systemd-resolved, which listens on a local loopback address (127.0.0.53). Docker cannot use this local address inside a container. If your host is also behind a corporate VPN or a restrictive firewall, the internal DNS bridge often fails to forward requests. This leaves your container stranded without a way to map domain names to IP addresses.
Three Ways to Fix It### 1. Set Global DNS in the Docker DaemonConfiguring the Docker daemon is the most effective long-term fix. It forces every container you build or run to use specific, reliable DNS providers like Google or Cloudflare.
- Open the Docker configuration file (you may need to create it):
sudo nano /etc/docker/daemon.json- Paste the following JSON to use Google's DNS (8.8.8.8) and Cloudflare (1.1.1.1):{ "dns": ["8.8.8.8", "1.1.1.1"] }- Restart Docker to apply the changes:``` sudo systemctl restart docker
### 2. Use the Host Network for the BuildSometimes you just need a quick fix without editing system files. You can bypass Docker's virtual network bridge entirely by using the `--network=host` flag. This makes the build process use your computer's network stack directly.
docker build --network=host -t my-image .
Keep in mind that while this is fast, it can be less secure in production environments. It is best used for local development or debugging stubborn connection issues.
### 3. Fix the systemd-resolved SymlinkCheck if your `/etc/resolv.conf` is pointing to the local stub resolver. Run this command:
ls -l /etc/resolv.conf
If the output shows `/run/systemd/resolve/stub-resolv.conf`, Docker is likely getting confused by the 127.0.0.53 address. You can fix this by linking `/etc/resolv.conf` to the actual network DNS settings instead:
sudo ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf
## Testing the FixDon't wait for a 10-minute build to fail again. Verify your DNS is working by running a tiny 5MB BusyBox image to perform a lookup:
docker run --rm busybox nslookup google.com
A successful fix will return a real IP address, such as `142.250.190.46`. If you still see a "can't resolve" error, your firewall might be blocking port 53.
## Pro-Tips for Complex NetworksCorporate environments often use custom subnets that clash with Docker's default range (usually 172.17.0.0/16). If your DNS works on home Wi-Fi but fails at the office, you might have an IP conflict. Tools like the [IP Subnet Calculator](https://toolcraft.app/en/tools/developer/ip-subnet-calculator) on ToolCraft can help you visualize these boundaries. This ensures your Docker bridge doesn't overlap with your company's physical DNS servers.
Lastly, check your proxy settings. If your company requires a proxy to reach the internet, DNS isn't your only hurdle. You must also pass the proxy variables into your build environment:
Add this to your Dockerfile if you are behind a proxy
ENV http_proxy http://10.0.0.5:8080 ENV https_proxy http://10.0.0.5:8080

