What is the principle of using docker to connect mysql host address instead of ip?

  docker, question

Using the PHP environment built by docker, the configuration file connects mysql and redis’ host addresses.For example, REDIS_HOST=redis, instead of ip forms such as 127.0.0.1, why is this possible?

REDIS_HOST=redis
 REDIS_PASSWORD=null
 REDIS_PORT=6379

Among them, the dockerfile file did not specify specifically, nor did the docker-compose.yml file see the relevant settings. The following is the definition of the redis container part of the docker-compose.yml file:

### Redis Container #########################################
 
 redis:
 build: ./redis
 volumes:
 - ${DATA_SAVE_PATH}/redis:/data
 ports:
 - "${REDIS_PORT}:6379"
 networks:
 - backend

I don’t know what principle this is. It should be related to docker. Does docker-compose define this redis service as “redis”, so redis can be used instead of ip to represent the host?

Redis in composer-compose is a service and a container, which is the same as specifying alias as host name when two containers are interconnected.