Solution to slow network request in docker container

Solution to slow network request in docker container

Several problems were discovered during the use of Docker. Network requests in Docker often failed, such as npm install and bundle install operations. Or, as an intermediate layer, the process of obtaining API data in the application often resulted in timeouts. Therefore, we began to explore the network mechanism of Docker to solve the problem of slow network requests.

1. Docker network mode

1. none

When configured as none, the Docker container network cannot have input or output and is isolated from the world.

2. Bridge
The default mode is bridge. Docker has its own virtual network card and obtains the network from the host through bridging.

3. Host
When specified as host, the host's network card is directly exposed to the container, and the container can access the Internet directly through the host's network. For example, if you want to get the redis service 127.0.0.1:6357 on the host, you have to use this method, but it is less secure.

4. Container
Using other container's network

2. Docker DNS resolution

The Docker container is essentially a Linux, so the DNS resolution method is the same as Linux, and the priority is to find the /etc/hosts file. Domain names such as localhost are written in this file, for example:

127.0.0.1 localhost

If the docker container links other containers, there will be an additional linked domain name, for example:

docker run --name app --link app-redis:redis -d ubuntu

There will be more in hosts

172.17.0.3 app-redis 038c8388e4a1

After finding the /etc/hosts file, then the /etc/resolv.conf file:

domain local
nameserver 192.168.65.1
nameserver 192.168.65.10

3. Solve the problem of slow network requests in docker containers

After packet capture and other analysis, it was found that the slow network request mainly occurred in DNS resolution, so DNS optimization was mainly adopted:

If the request is for your own intranet API, you can directly modify the /etc/hosts file. If the request is for an external network, you can change the nameserver in /etc/resolv.conf.

The docker container is definitely not implemented directly by modifying files, but can be implemented through the run command:

# Add host
docker run --name app --add-host='api.embbnux.com:10.98.10.98' -d ubuntu
# Specify DNS server
docker run --name app --dns=223.5.5.5 --dns=8.8.8.8 -d ubuntu

In this way, the DNS resolution time in the Docker container is accelerated.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Docker intranet builds DNS and uses domain name access instead of ip:port operation
  • How to directly access the docker for windows container intranet through an independent IP
  • Method for accessing independent IP in Docker container
  • How to build a private server in docker (docker-registry with nginx&ssl on centos)
  • Detailed explanation of building lanproxy intranet penetration service based on docker
  • Steps to deploy multiple tomcat services using DockerFile on Docker container
  • Docker image creation, uploading, pulling and deployment operations (using Alibaba Cloud)
  • Analysis of the implementation process of Docker intranet penetration frp deployment

<<:  CentOS 6.4 MySQL 5.7.18 installation and configuration method graphic tutorial

>>:  React implements a highly adaptive virtual list

Recommend

Detailed explanation of the correct use of the if function in MySQL

For what I am going to write today, the program r...

Tips on HTML formatting and long files for web design

<br />Related articles: 9 practical suggesti...

Teach you how to deploy zabbix service on saltstack

Table of contents Saltstack deploys zabbix servic...

Pull-down refresh and pull-up loading components based on Vue encapsulation

Based on Vue and native javascript encapsulation,...

Three strategies for rewriting MySQL query statements

Table of contents Complex query and step-by-step ...

Why node.js is not suitable for large projects

Table of contents Preface 1. Application componen...

Detailed example code of mysql batch insert loop

background A few days ago, when I was doing pagin...

Detailed explanation of the frame and rules attributes of the table in HTML

The frame and rules attributes of the table tag c...

Some suggestions for improving Nginx performance

If your web application runs on only one machine,...

Complete steps of centos cloning linux virtual machine sharing

Preface When a Linux is fully set up, you can use...

How to open ports to the outside world in Alibaba Cloud Centos7.X

In a word: if you buy a cloud server from any maj...

Mysql SQL statement operation to add or modify primary key

Add table fields alter table table1 add transacto...

What is MIME TYPE? MIME-Types type collection

What is MIME TYPE? 1. First, we need to understand...