Table of Contents
Problem description:
问题解决
问题补充:
Home Operation and Maintenance Docker What should I do if docker cannot ping the host?

What should I do if docker cannot ping the host?

Jan 04, 2022 pm 04:58 PM
docker ping

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
Copy after login

  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"}
Copy after login
 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: <div class="code" style="position:relative; padding:0px; margin:0px;"><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><div class="contentsignin">Copy after login</div></div>  Use the command

vim /etc/sysctl.conf

Edit the configuration file and add the following code to the file: <div class="code" style="position:relative; padding:0px; margin:0px;"><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><div class="contentsignin">Copy after login</div></div>  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
Copy after login

  添加网桥:

brctl addbr br0
Copy after login

  添加ip字段:

ip addr add 172.16.0.1/24 dev br0
Copy after login

  启用网桥br0

ip link set dev br0 up
Copy after login

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

  修改docker默认网桥:

vim /etc/docker/daemon.json
Copy after login

  添加字段:

"bridge":"br0"
Copy after login

  重启docker

service docker start
Copy after login

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

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

docker run -itd --name busy_test busybox
Copy after login

  查看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 of this Website
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

Hot AI Tools

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.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to package project with pycharm How to package project with pycharm Apr 25, 2024 am 03:54 AM

There are four ways to package a project in PyCharm: Package as a separate executable file: Export to EXE single file format. Packaged as an installer: Generate Setuptools Makefile and build. Package as a Docker image: specify an image name, adjust build options, and build. Package as a container: Specify the image to build, adjust runtime options, and start the container.

PHP distributed system architecture and practice PHP distributed system architecture and practice May 04, 2024 am 10:33 AM

PHP distributed system architecture achieves scalability, performance, and fault tolerance by distributing different components across network-connected machines. The architecture includes application servers, message queues, databases, caches, and load balancers. The steps for migrating PHP applications to a distributed architecture include: Identifying service boundaries Selecting a message queue system Adopting a microservices framework Deployment to container management Service discovery

Docker completes local deployment of LLama3 open source large model in three minutes Docker completes local deployment of LLama3 open source large model in three minutes Apr 26, 2024 am 10:19 AM

Overview LLaMA-3 (LargeLanguageModelMetaAI3) is a large-scale open source generative artificial intelligence model developed by Meta Company. It has no major changes in model structure compared with the previous generation LLaMA-2. The LLaMA-3 model is divided into different scale versions, including small, medium and large, to suit different application needs and computing resources. The parameter size of small models is 8B, the parameter size of medium models is 70B, and the parameter size of large models reaches 400B. However, during training, the goal is to achieve multi-modal and multi-language functionality, and the results are expected to be comparable to GPT4/GPT4V. Install OllamaOllama is an open source large language model (LL

Agile development and operation of PHP microservice containerization Agile development and operation of PHP microservice containerization May 08, 2024 pm 02:21 PM

Answer: PHP microservices are deployed with HelmCharts for agile development and containerized with DockerContainer for isolation and scalability. Detailed description: Use HelmCharts to automatically deploy PHP microservices to achieve agile development. Docker images allow for rapid iteration and version control of microservices. The DockerContainer standard isolates microservices, and Kubernetes manages the availability and scalability of the containers. Use Prometheus and Grafana to monitor microservice performance and health, and create alarms and automatic repair mechanisms.

Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node? Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node? Mar 05, 2025 pm 05:57 PM

Detailed explanation and installation guide for PiNetwork nodes This article will introduce the PiNetwork ecosystem in detail - Pi nodes, a key role in the PiNetwork ecosystem, and provide complete steps for installation and configuration. After the launch of the PiNetwork blockchain test network, Pi nodes have become an important part of many pioneers actively participating in the testing, preparing for the upcoming main network release. If you don’t know PiNetwork yet, please refer to what is Picoin? What is the price for listing? Pi usage, mining and security analysis. What is PiNetwork? The PiNetwork project started in 2019 and owns its exclusive cryptocurrency Pi Coin. The project aims to create a one that everyone can participate

How to install deepseek How to install deepseek Feb 19, 2025 pm 05:48 PM

There are many ways to install DeepSeek, including: compile from source (for experienced developers) using precompiled packages (for Windows users) using Docker containers (for most convenient, no need to worry about compatibility) No matter which method you choose, Please read the official documents carefully and prepare them fully to avoid unnecessary trouble.

How to optimize the performance of Java functions through containerization? How to optimize the performance of Java functions through containerization? Apr 29, 2024 pm 03:09 PM

Containerization improves Java function performance in the following ways: Resource isolation - ensuring an isolated computing environment and avoiding resource contention. Lightweight - takes up less system resources and improves runtime performance. Fast startup - reduces function execution delays. Consistency - Decouple applications and infrastructure to ensure consistent behavior across environments.

Deploy JavaEE applications using Docker Containers Deploy JavaEE applications using Docker Containers Jun 05, 2024 pm 08:29 PM

Deploy Java EE applications using Docker containers: Create a Dockerfile to define the image, build the image, run the container and map the port, and then access the application in the browser. Sample JavaEE application: REST API interacts with database, accessible on localhost after deployment via Docker.

See all articles