SoFunction
Updated on 2025-04-13

Detailed explanation of Docker's problem of not being able to connect to Docker Hub and mirror accelerator

Preface

Today I encountered a relatively common problem. Although I tried many methods but still failed to solve it (because the problem through consultation is on the cloud host, and I can only wait for it to solve it there), it summarized all the steps and test solutions involving multiple aspects, including DNS configuration, network connectivity, firewall settings, etc.

Problem description

When we try to pullalpineWhen mirroring, I received the following error message:

Error response from daemon: Get "/v2/": net/http: request canceled while waiting for connection ( exceeded while awaiting headers)

At the same time, usecurlThe test directly accessing the HTTPS endpoint of Docker Hub also failed:

curl -v /v2/

Output displayConnection refusedandNetwork is unreachablemistake.

Diagnostic process

1. Check DNS configuration

First, check whether the currently configured DNS server can resolve the required domain name. usenslookupCommand test:

nslookup 

The results show that the currently configured DNS server cannot resolve the domain name of some domestic mirror accelerators, but it can be parsed.

2. Replace the DNS server

Modifying the system's DNS settings can help resolve network connectivity issues due to DNS resolution issues. On Linux systems, you can modify/etc/File to change the DNS server address. However, in many modern Linux distributions, edit directly/etc/May not take effect, because the file may be served by the system (e.g.NetworkManagerorsystemd-resolved)manage.

Here are several common ways to modify DNS settings:

Method 1: Edit directly /etc/

This method is suitable for those who do not use itNetworkManagerorsystemd-resolvedsystem. Please note that if your system uses these services, edit them directly/etc/May be overwritten after restart.

  • Open using a text editor/etc/document:

    sudo nano /etc/
  • Add or modify the DNS server address. For example, add Google's public DNS server:

    nameserver 8.8.8.8
    nameserver 8.8.4.4
  • Save and close the file.

Method 2: Modify through NetworkManager

If the system is usedNetworkManager, you can modify the DNS settings through the following steps:

  • Open the terminal and list all network interfaces:

    nmcli device show
  • Find the network interface name you want to configure (for exampleeth0), and then set up DNS with the following command:

    sudo nmcli con modify "Your connection name"  "8.8.8.8 8.8.4.4"
    sudo nmcli con modify "Your connection name" -auto-dns yes
    sudo nmcli con up "Your connection name"

    in"Your connection name"It's you fromnmcli connection showThe network connection name found in the command output.

Method 3: Modify through systemd-resolved

If the system is usedsystemd-resolved, can be edited/etc/systemd/File to set global DNS:

  • edit/etc/systemd/document:

    sudo nano /etc/systemd/
  • turn up[Resolve]Part, and add or modify the following lines:

    [Resolve]
    DNS=8.8.8.8 8.8.4.4
  • Save and close the file.

  • Restartsystemd-resolvedServices to apply changes:

    sudo systemctl restart systemd-resolved
  • If your system is still used/etc/, you may need to link it tosystemd-resolvedProvided stub file:

    sudo ln -sf /run/systemd/resolve/ /etc/

Method 4: Set DNS separately for the Docker daemon

If you want to set up a DNS server for Docker containers only, you can use Docker's configuration file/etc/docker/Configure:

  • Edit or create/etc/docker/document:

    sudo nano /etc/docker/
  • Add or modify the following:

    {
        "dns": ["8.8.8.8", "8.8.4.4"]
    }
  • Save and close the file.

  • Reload the Docker daemon to apply the changes:

    sudo systemctl daemon-reload
    sudo systemctl restart docker

By one of the above methods, you should be able to successfully modify the system's DNS settings. Just select the method that best suits your system configuration to operate

3. Test the new DNS configuration

Confirm that the new DNS configuration is in effect and can resolve the required domain name:

nslookup 

If these commands return valid IP addresses, the DNS configuration is correct. Test using the parsed IP address:

curl -v https://<IP_ADDRESS>:443/v2/

If you still cannot connect using the IP address directly, it may be that the target server has access restrictions on some IP ranges, or the service is temporarily unavailable.

4. Check network connectivity again

usetelnetorncCommand to test port connectivity:

telnet  443
# ornc -zv  443

If these commands show that the connection cannot be connected or timed out, it may be because a firewall or other network security settings block outbound HTTPS requests, not just connection issues with Docker Hub.

5. Use Alibaba Cloud Mirror Accelerator

If there is no problem with DNS and basic network connectivity, but still cannot download the image through Docker Hub, it is recommended to use Alibaba Cloud's mirror accelerator to pull the image:

docker pull /library/alpine:latest

6. Check firewall and security group rules

Finally, make sure the firewall rules on the host and the cloud service provider's security group settings allow outbound HTTPS traffic (port 443). View iptables rules on the host:

sudo iptables -L -n | grep 443

Meanwhile, log in to the cloud console and check the security group rules associated with your instance to ensure outbound HTTPS traffic is allowed.

Summarize

By stepping through DNS configuration, network connectivity, and firewall settings, some Docker containers cannot connect to Docker Hub or mirror accelerator. Here are the key steps:

  • Check and replace the DNS server: Ensure that all necessary domain names can be resolved.
  • Test network connectivity: Confirm that HTTPS connection to Docker Hub can be established.
  • Using mirror accelerator: Try to use domestic mirror accelerator to download the mirror.
  • Check firewall and security group rules: Make sure that outbound HTTPS requests are not blocked.

This is the article about this detailed explanation of the problem of Docker not being able to connect to Docker Hub and image accelerator. For more related content about Docker not being able to connect to Docker Hub and image accelerator, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!