search
HomeOperation and MaintenanceDockerWhat should I do if docker cannot ping the host?

Solutions to the problem that docker cannot ping the host: 1. Modify daemon.json; 2. Turn off the firewall; 3. Modify sysctl.conf; 4. Reset the network bridge.

What should I do if docker cannot ping the host?

The operating environment of this article: ubuntu16.04 system, Docker 20.10.11, Dell G3 computer.

What should I do if docker cannot ping the host?

Docker bridge mode cannot ping the host

Problem description:

 DockerThe network mode is divided into four types. Generally, when we do not set it, the default is bridgesingle bridge mode, the container uses an independent network Namespace and is connected to the docker0 virtual network card. Communicate with the host through the docker0 bridge and Iptables nat table configuration.
  At this time, test on the bastion machine and use busybox to test:

# 拉取镜像
docker pull busybox
# 运行容器
docker run -itd --name busy_bridge busybox

  Instruction docker network inspect bridge Check the network:
What should I do if docker cannot ping the host?

## The network configuration is successful. Go inside the container and check

ip. You can see that ip has been allocated, but ping fails when pinging the external network and cannot Connect to the external network:
What should I do if docker cannot ping the host?

 But when you conduct the same test locally or on Alibaba Cloud, you find that you can connect to the network. What is the problem?

Problem Analysis:

After finding information on the Internet, many people restart

docker, and then they can connect. Usually it is because a certain configuration is modified and then Restarting works, it has no effect here. Generally, there are several types of modifications. Try them one by one below:

  1. Modifydaemon.json
The container cannot access the host because the network segment allocated by the bridge conflicts with the host. You need to modify

daemon.json to specify the allocation. Use the command vim /etc/docker/daemon.jsonAdd after entering:

{"bip":"172.16.10.1/24"}
 Although you can access it by restarting

docker and creating a container, there is no conflict at all between the ip assigned by the original bastion host and the container. The method doesn’t work.

  1. Turn off the firewall
The container cannot access the host through the bridge, and therefore cannot access the external network. The firewall may be blocking access, so you can turn it off Firewall or open a certain port. Tested on the server, turned on the firewall, and found that the container was indeed unable to access the Baidu homepage and the host. After closing the firewall and restarting

docker, the container could be accessed normally.  However, the firewall on the bastion machine is originally turned off, so this method is useless.

  1. Modifysysctl.conf
  2. ##  
docker

The internal network of the host is normal, and the network with other hosts is normal. The connection fails. Other hosts cannot connect to the port mapped on the docker host, and docker cannot connect to external hosts internally. Use the docker info command to check the information and find the following error: <pre class="brush:php;toolbar:false">WARNING: IPv4 forwarding is disabled WARNING: bridge-nf-call-iptables is disabled WARNING: bridge-nf-call-ip6tables is disabled</pre>  Use the command

vim /etc/sysctl.conf

Edit the configuration file and add the following code to the file: <pre class="brush:php;toolbar:false">net.bridge.bridge-nf-call-ip6tables=1 net.bridge.bridge-nf-call-iptables=1 net.bridge.bridge-nf-call-arptables=1 net.ipv4.ip_forward=1</pre>  Then use the command

systemctl restart network

Restart the network and check docker info again, the warning disappears. But it's still useless. The container on the bastion machine still cannot access the host machine through the bridge and cannot access the external network.

    Reset the bridge
  1. After using the command
yum install bridge-utils

to install the tool, use brctl show Check the network bridge and you can find:
Use the What should I do if docker cannot ping the host?docker network create [bridge name]
command to create a new network bridge and find the generated bridge id is still 8000.0000000000, create a container on the new bridge, and check again and there is no change, indicating that it is probably a problem with the bridge.  Test again. At this time, the bridge ip
is 172.17.0.1 and the container ip is 172.0.0.2. The host function is found. ping The network bridge is connected, but the container cannot be connected. The container cannot connect to the network bridge and cannot connect to the host, let alone the external network, so there must be a problem with the network bridge. <h3 id="问题解决">问题解决</h3> <p>  这里<code>docker network生成新的网桥不行,说明dockernetwork存在问题,我们利用刚才下载的bridge-utils来创建网桥。
  首先暂停docker服务,利用指令:

service docker stop

  添加网桥:

brctl addbr br0

  添加ip字段:

ip addr add 172.16.0.1/24 dev br0

  启用网桥br0

ip link set dev br0 up

  查看网络br0
What should I do if docker cannot ping the host?

  修改docker默认网桥:

vim /etc/docker/daemon.json

  添加字段:

"bridge":"br0"

  重启docker

service docker start

  此时查看网桥:
What should I do if docker cannot ping the host?

  在没有挂载容器前,依旧是8000.000000000000。运行测试容器:

docker run -itd --name busy_test busybox

  查看What should I do if docker cannot ping the host?:
What should I do if docker cannot ping the host?

  此时容器挂载在网桥上了,再次查看网桥id
What should I do if docker cannot ping the host?

  说明已经其作用,进入测试容器内部,What should I do if docker cannot ping the host?:
What should I do if docker cannot ping the host?

  成功!
  补充:这里使用docker network新建网桥,没有用,发现新建网桥挂载容器后,其bridge id依旧不变,没有起作用,说明堡垒机上的docker network可能存在问题。

问题补充:

  上面的问题是创建自定义网桥,然后在自定义网桥上连接容器ab,结果宿主机无法pingab,且进入容器内部后,两个容器无法ping通自定义网络,但能彼此相通。
  查了很多资料,发现了这篇文章。博主说问题原因是系统内核的网桥模块bridge.ko加载失败导致,解决问题的方案是升级内核或升级系统。
  升级centos内核参考这篇。
  升级完成后,重装Docker,自定义网桥和容器,成功!不再有网络问题。

推荐学习:《docker视频教程

The above is the detailed content of What should I do if docker cannot ping the host?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How to handle secrets in Docker?How to handle secrets in Docker?Aug 27, 2025 am 01:27 AM

DockerSwarm should be given priority when using DockerSwarm. It is stored in the Raft log of the Swarm management node through encryption and is only provided to authorized services. It is mounted in /run/secrets in the container in the form of a memory file. The creation command is echo "password"|dockersecretcreatedb_password-. When the service is created, access permission is specified through --secretdb_password, and the value is obtained by reading /run/secrets/db_password in the application. The advantages are transmission and encryption at rest and access are controlled. The disadvantages are only Swarm mode.

How to commit changes to a Docker image?How to commit changes to a Docker image?Aug 27, 2025 am 12:52 AM

You can use the dockercommit command to save the changes of the container as a new image, but Dockerfile should be used in production environments first; the specific steps are: 1. Run and modify the container, such as installing curl in ubuntu:20.04; 2. Get the container ID through dockerps in another terminal and execute dockercommitmy-new-image:latest to create a new image; 3. Use the new image to run the container to verify the changes; in addition, you can add the -m and -a parameters to write the commit information and author; it should be noted that dockercommit only saves file system changes, does not include the runtime status of volumes, networks, etc., and will cause non-repeated construction, so it is not recommended to use it.

What is Docker Hub?What is Docker Hub?Aug 26, 2025 am 08:10 AM

DockerHubisacloud-basedregistryserviceforstoring,sharing,andmanagingDockercontainerimages.1.Itactsasanimagerepositorywhereuserscanstoreandpullimagesusingtagslikedockerpullnginx:latest.2.ItprovidesOfficialandVerifiedImagesfromtrustedsourcessuchassoftw

How to get help with docker commandsHow to get help with docker commandsAug 26, 2025 am 03:06 AM

Usedocker--helpforgeneralcommandlistsanddockerrun--helpforspecificcommanddetails,bothprovidingversion-specific,offline-accessibleinformation.2.ConsulttheofficialDockerDocumentationforcomprehensiveguides,tutorials,andexamplesonallDockerfeatures.3.Rund

How to pass environment variables to a docker containerHow to pass environment variables to a docker containerAug 25, 2025 pm 03:45 PM

Usethe-eflagtopassindividualenvironmentvariablesatruntime;2.Use--env-filetoloadmultiplevariablesfroma.envfile;3.DefinedefaultvaluesintheDockerfilewithENV;4.Specifyvariablesindocker-compose.ymlorviaenv_fileinDockerCompose;5.Dynamicallypasshostvariable

How to share data between docker containersHow to share data between docker containersAug 25, 2025 pm 03:01 PM

UsenamedDockervolumestosharedatabetweencontainers,createdviadockervolumecreateandmountedincontainersusing-vshared-data:/app/data.2.Bindmountsallowsharinghostdirectoriesbymountingthemintocontainerswith-v/host/shared:/app/data,suitablefordevelopment.3.

How to use an overlay network in dockerHow to use an overlay network in dockerAug 25, 2025 pm 01:59 PM

Initializeaswarmwith"dockerswarminit"toenableoverlaynetworking.2.Createanoverlaynetworkusing"dockernetworkcreate--driveroverlay"andoptionallyaddencryptionandcustomsubnets.3.Deployservicesvia"dockerservicecreate"orDockerC

How to build a Docker image from a Dockerfile?How to build a Docker image from a Dockerfile?Aug 25, 2025 am 12:19 AM

InstallandverifyDockerusingdocker--versionanddockerinfo.2.CreateaDockerfilewithinstructionslikeFROM,COPY,RUN,andCMDinyourprojectdirectory.3.Buildtheimageusingdockerbuild-tyour-image-name:tag.withthedotspecifyingthebuildcontext.4.Confirmsuccesswithdoc

See all articles

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Hot Topics