Docker-compose networks principle

We talked about how to connect containers in the previous section
A network that shares the same third container,
To use localhost to access the endpoints of other containers. The main principle is the namespace of Linux network
However, this problem is different in docker compose
Start the two containers through the following script and configure them to share a network called unetwork

version: '3'
services:
  utest1:
    image: nginx
    networks:
      - unetwork

  utest2:
    image:  ubuntu
    command: sleep infinity
    networks:
      - unetwork

networks:
  unetwork:
    driver: bridge
    ipam:
      config:
        - subnet: 192.168.100.0/24

you can see

  • Containers cannot be accessed through localhost, that is, they do not share a network namespace
  • Check /etc/ hosts and find that the container name is not written into DNS like link
  • Curl utest 1:80 accessed nginx

Docker compose can use a variety of networks
For details, please refer to

https://docs.docker.com/engine/reference/commandline/network_create/

If there is no special configs, the bridge pattern is generated here

Send a Message