Docker lnmp domain name resolution scheme

  docker, question

Problem description

In the process of development and testing, people often have to get all kinds of test domain names. How to manage domain name resolution gracefully through the lnmp environment established by docker-compose?

Only the docker-compose method is considered, and docker run … is not considered. docker run can specify –dns=xxx, or dns is set in docker’s default configuration, etc.

Start dnsmasq service in docker-compose, specify fixed ip for all services, set dns to dnsmasq ip in php container,
However, after docker-compose up, it can be seen that the IP setting of the container is normal, but dns of php container is still the default 127.0.0.11.
Not 172.25.0.5 as we specified
图片描述
图片描述

The key contents of docker-compose.yml are as follows:

Related codes

version: "3"
 services:
 dnsmasq:
 image: jpillora/dnsmasq:latest
 restart: always
 networks:
 dnmp_net:
 ipv4_address: 172.25.0.5
 ports:
 - "53:53"
 - "5380:8080"
 volumes:
 - ./dnsmasq/dnsmasq.conf:/etc/dnsmasq.conf:rw
 environment:
 - HTTP_USER = admin
 - HTTP_PASS = xxxxxx
 
 php72:
 build:
 context: .
 args:
 PHP_VERSION: ${PHP72_VERSION}
 PHP_XDEBUG: ${PHP72_XDEBUG}
 PHP_SWOOLE: ${PHP72_SWOOLE}
 PHP_REDIS: ${PHP72_REDIS}
 REPLACE_SOURCE_LIST: ${REPLACE_SOURCE_LIST}
 volumes:
 - ${SOURCE_DIR}:/var/www/html/:rw
 - ${PHP72_PHP_CONF_FILE}:/usr/local/etc/php/php.ini:ro
 - ${PHP72_FPM_CONF_FILE}:/usr/local/etc/php-fpm.d/www.conf:rw
 restart: always
 cap_add:
 - SYS_PTRACE
 networks:
 dnmp_net:
 ipv4_address: 172.25.0.11
 dns:
 - 172.25.0.5
 
 networks:
 dnmp_net:
 driver: bridge
 ipam:
 driver: default
 config:
 - subnet: 172.25.0.0/24

The environmental background of the problem and what methods have you tried?

1. Enter the container to modify the container’s /etc/hosts
2. Enter/etc/resolve.conf of the container modification container, pointing to the customized dns server
3. Start dnsmasq service in docker-compose, specify fixed ip for all services, and set dns as dnsmasq ip in php container

How do you manage domain name resolution in the docker test environment? Is there a scientific posture of 0.0

Do i have to use ip? The container_name of the container created by docker-compose is the host domain name. If you use link or depends_on, you can also set an alias as the host domain name. Ip in container, thankless