This is a hard one but it would be great to solve it and my configuration would awesome!
I have two docker-compose projects: no 1. contains configuration for traefik
, no 2. contains my-service
that has http://my-domain.tld
as parameter.
The goal is to call (let's say ping) http://my-domain.tld
from my-service
and get through to traefik
. Both services now are in the same network but I need somehow to pass configuration that http://my-domain.tld
points to traefik
.
# Project no 1. configuration for traefik that I want to access from no 2. project
version: '3.7'
services:
traefik:
container_name: traefik
networks:
- default
networks:
default:
name: traefik
# Project no 2.
version: '3.7'
services:
my-service:
environment:
- URL=http://my-domain.tld
networks:
- traefik
networks:
traefik:
external:
name: traefik
Tried solutions:
Variant a)
Does not work. When I call http://my-domain.tld
it is directed to first node in traefik
network (in my case it was my-service
itself). Difference from original: aliases
in network section.
# Project no 2.
version: '3.7'
services:
my-service:
environment:
- URL=http://my-domain.tld
networks:
traefik:
aliases:
- ${MERCURE_DOMAIN}
networks:
traefik:
external:
name: traefik
Variant b)
Fails because traefik
that service depends on is not in the project/network: Service 'my-service' depends on service 'traefik' which is undefined.
Differences from original: depends_on
and extra_hosts
.
# Project no 2.
version: '3.7'
services:
my-service:
environment:
- URL=http://my-domain.tld
networks:
- traefik
depends_on:
- traefik
extra_hosts:
- http://my-domain.tld:traefik
networks:
traefik:
external:
name: traefik
Variant c)
Works! However keep in mind that I had to hardcode IP address of traefik
service. This is not the worst scenario but it makes project less portable and I want to avoid it. Differences from original: network configuration for no 1. project, static ip for traefik
service and extra_hosts
for no 2. project.
# Project no 1. configuration for traefik that I want to access from next project
version: '3.7'
services:
traefik:
container_name: traefik
networks:
default:
ipv4_address: 172.133.239.10
networks:
default:
name: traefik
driver: bridge
ipam:
driver: default
config:
- subnet: 172.133.239.0/24
# Project no 2.
version: '3.7'
services:
my-service:
environment:
- URL=http://my-domain.tld
networks:
- traefik
extra_hosts:
- http://my-domain.tld:172.133.239.10
networks:
traefik:
external:
name: traefik
Variant d)
Is waiting for your suggestion how to make this perfect!
EDIT: rewrote docker-compose configuration to separate cases to show what I have tried and accomplished because there were some confusion about it.