SoFunction
Updated on 2025-03-10

Issues that the network cannot be accessed when docker builds the image

Preface

In usedocker buildCommand buildDockerWhen encountering the situation where the network cannot be connected during mirroring, there may be many situations.

Check host network settings

Check if your host is configured with a proxy server or firewall, which may prevent itDockerContainers access external network.

If your host uses a proxy server, make sureDockerThe container has properly configured proxy settings.

If your host has a firewall running, please allowDockerContainers access external network.

Test the host's network connection

ping 

Test whether the host can access external network.

If the host cannot access the external network, it may be caused by a host network setup error or a network failure.

Test the host's DNS resolution

nslookup 

Test whether the host can successfully resolve the domain name toIPaddress.

ifDNSThe resolution failed, which may be due to the host's domain name server setting error orDNSA result of a server failure.

Test the proxy server connection of the host

If you have configured the proxy service, you can test it through the following, which will useHTTPProxy server connection towebsite.

You need toandportReplace with the actual proxy server address and port number.

If the connection is successful, it means the host's proxy server is set correctly.

curl -x :port 

Check Docker network settings

CheckDockerNetwork List

docker network ls

This will list all availableDockernetwork.

By default,DockerThree networks are created during installation:bridgehostandnone

CheckDockerNetwork details

docker network inspect <network-name>

This will display the specifiedDockerNetwork details, including networkIPAddress range, gateway, domain name server, etc.

Test Docker network connection

docker run --rm --network=<network-name> alpine ping <ip-address>

This will be specified in theDockerRun one in the networkAlpineContainer and usepingCommand test specificationIPThe connection of the address.

You need to<network-name>Replace with actualDockerNetwork name,<ip-address>Replace with the IP address to be tested.

If the connection is successful, it meansDockerThe network settings are correct.

The sample code is as follows:

docker run --rm --network=host alpine ping 

testdockerUsing the host machinehostRun one in the network situationAlpineContainer and usepingCommand Test

Proxy server configuration

If your host is using a proxy server, make sureDockerThe container has properly configured proxy settings.

For example, you can add the following command toDockerfileIn the file:

ENV http_proxy ":port"
ENV https_proxy ":port"

This willDockerSettings in containersHTTPandHTTPSProxy server.

You need toandportReplace with the actual proxy server address and port number.

There is another possibility that you need to configure the mirror source to access packages in mainland China faster:

RUN sed -i s@//@//@g /etc/apt/
RUN sed -i s@//@//@g /etc/apt/
RUN sed -i s@//@//@g /etc/apt/
RUN sed -i s@//@//@g /etc/apt/

Rebuild the image

If you have done the above steps but still can't connect to the internet, try rebuilding the image.

It is best to clear the old mirror cache before rebuilding to ensureDockerUse the latestDockerfileFiles and configurations.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.