This is the 11th day of my participation in the August More Text Challenge

Life is too short to learn Python together

preface

In a Python Web project, such as Django Web Service, the service must communicate with MySQL Redis, etc. When docker is deployed, the containers are isolated from each other. How do they communicate? How can the external access services running in the container on the host machine from the host machine? This paper introduces the network communication mechanism of Docker, and explains how to access each other between containers and between containers as well as between containers and host machines through this mechanism.

Docker network communication mechanism

The network communication mechanism provided by Docker is to allow network services to be provided through external access to containers or container interconnection.

When Docker starts up, it automatically creates a virtual bridge on the host, which can be understood as a software switch. It will be mounted to its network port for forwarding. Docker will also randomly assign an address of a local private network segment that is not occupied to the Bridge interface when it is started. After that, the network port of the container started with this bridge will also automatically assign an address of the same network segment. When a Docker container is created, a pair of Veth interfaces will be created at the same time. One part of the interface is inside the container, namely eth0, and the other part is mounted to the bridge, whose name starts with VEth. When packets are sent to one interface, the other interface can also receive the same packet. Containers can also communicate with each other, and Docker creates a virtual shared network between hosts and all containers.

The advantages and disadvantages of the docker default bridge is embodied in the same point, is all containers can communicate with each other, the problem is that if a certain two several containers frequent data exchange is bound to affect other containers, so can build their own Bridges, a project should build a bridge.

Docker network use

Generally when we were in a deployment project will create a bridge for the project, rather than use the default, if each application using the default bridge, means can be communicate between each container, in the actual project, we only need to use to make this program can communicate between container is ok.

Generally, container communication is carried out from an application perspective when docker bridge is used to realize container to container communication.

Docker network LS: View the configuration of the Docker bridge

Docker create bridge: Docker creates a custom bridge

Docker run -d -p 8080:8080 –name xx –network XXX (docker run –network must exist) Once you specify a bridge when you start a container, you can later use the container name in any container associated with this bridge to communicate with other containers. The container name represents the IP address of the container. That is, services on the same bridge can access each other, either using the form http://ip:port or http:// container name :port, because the container name is unique.

Docker network RM Bridge name: Delete the bridge

Docker inspect inspect for details of the bridge

conclusion

The article was first published in the wechat public account Program Yuan Xiaozhuang, at the same time in nuggets.

The code word is not easy, reprint please explain the source, pass by the little friends of the lovely little finger point like and then go (╹▽╹)