I think it's better to make a new thread with details for my problem. I hope it's ok.
My goal is to have a Traefik Container with dnsmasq, and multiple project Container with a reference to my Traefik. For the projects, I use nginx and a separate php container.
The dnsmasq resolve the test domain to traefik container
RUN echo "address=/.test/172.20.20.30" > /etc/dnsmasq.d/test_domain.conf
My Traefik docker-compose.yml
networks:
traefik:
name: traefik
ipam:
driver: default
config:
- subnet: "172.20.20.0/24"
services:
traefik:
image: traefik:3.0
container_name: traefik
ports:
- "80:80"
- "443:443"
volumes:
- ....
depends_on:
- dnsmasq
networks:
traefik:
ipv4_address: 172.20.20.30
dnsmasq:
build:
context: .
dockerfile: .....
container_name: dnsmasq
networks:
traefik:
ipv4_address: 172.20.20.20
My shortened project docker-compose.yml
networks:
container1_network:
traefik:
external: true
services:
nginx:
build:
context: .
dockerfile: .....
container_name: container1-nginx
labels:
- "traefik.enable=true"
- "traefik.http.routers.container1.rule=Host(`container1.test`) || Host(`www.container1.test`)"
- "traefik.http.routers.container1.entrypoints=webSecure"
- "traefik.http.routers.container1.tls=true"
- .......
- "traefik.http.routers.container1.service=container1"
- "traefik.http.services.container1.loadbalancer.server.port=80"
- "traefik.docker.network=traefik"
networks:
- traefik
- container1_network
php:
build:
context: .
dockerfile: .....
container_name: container1-php
restart: unless-stopped
networks:
- container1_network
This works great. I have my test domains (local machine has dnsmasq with defined for test domain to 127.0.0.1)
Now i have PHP Scripts, that must resolves my domainnames like container1.test. But this failed because the container resolves the test domain to localhost
ping container1.test
PING container1.test (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: seq=0 ttl=64 time=0.014 ms
My Idea was, to add the dns from traefik to PHP Container:
php:
build:
context: .
dockerfile: .....
container_name: container1-php
restart: unless-stopped
dns:
- 172.20.20.20
networks:
- traefik
- container1_network
Now I can resolve my test Domain with PHP. But on each request, I get an other project in my browser under container1.test. This is because the containers from different project can communicate between them from the external network.
One solution is to rename my services to container1-php, container1-nginx,...
Is this the best option, or does anyone have any other ideas?